Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FAIL_ON_NULL_FOR_PRIMITIVES failure does not indicate field name in exception message #2101

Closed
raderio opened this issue Jul 30, 2018 · 8 comments
Milestone

Comments

@raderio
Copy link

raderio commented Jul 30, 2018

public class Test {

    private String type;
    private long num;

    @JsonCreator
    public Test(@JsonProperty(value = "type", required = true) String type,
                @JsonProperty(value = "num", required = true) long num) {
        this.type = type;
        this.num = num;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public long getNum() {
        return num;
    }

    public void setNum(long num) {
        this.num = num;
    }
}
ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);

        try {
            String json = "{ \"type\" : \"number\", \"num\":null }";
            Test car = objectMapper.readValue(json, Test.class);

            System.out.println(car.getNum());
        } catch (MismatchedInputException ex) {
            ex.getPath().forEach(it -> System.out.println("field: " + it.getFieldName()));
        }

shows field: num
but missing field in json

ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, true);

        try {
            String json = "{ \"type\" : \"number\" }";
            Test car = objectMapper.readValue(json, Test.class);

            System.out.println(car.getNum());
        } catch (MismatchedInputException ex) {
            ex.getPath().forEach(it -> System.out.println("field: " + it.getFieldName()));
        }

shows nothing

Please add filed name and in this case.

@raderio raderio changed the title FAIL_ON_NULL_FOR_PRIMITIVES not capture filed name FAIL_ON_NULL_FOR_PRIMITIVES not capture field name Jul 30, 2018
@cowtowncoder
Copy link
Member

Thank you for reporting the issue: it does look like a bug.

@cowtowncoder
Copy link
Member

Actually, I was wrong: it is not a bug -- path refers to logical context within JSON content, and there is no property in latter case that triggers the exception: it is the lack of content that triggers it.
While information on missing logical property (distinct from JSON property in input) would be useful, that is not what path reference tracks.

cowtowncoder added a commit that referenced this issue Sep 7, 2018
@raderio
Copy link
Author

raderio commented Sep 7, 2018

While information on missing logical property (distinct from JSON property in input) would be useful

Can you please than add a new property which will hold the name? It will be really useful when to send a response to client, and indicate what fields has errors.

@cowtowncoder
Copy link
Member

@raderio Actually, come to think of it bit more, I wonder if I was wrong. Reference mentions "field" or "key", in Java context, and not in input source.

I'll have to think bit more about this after all, since that being the case, it is not unreasonable it would be populated if there is sufficient information to do so.

@raderio
Copy link
Author

raderio commented Sep 10, 2018

Thanks.

@neetkee
Copy link

neetkee commented Sep 2, 2019

We have a similar problem. It would be great to have the same behavior with fields of different types.

@cowtowncoder cowtowncoder added 2.10 and removed 2.9 labels Sep 4, 2019
@cowtowncoder cowtowncoder changed the title FAIL_ON_NULL_FOR_PRIMITIVES not capture field name FAIL_ON_NULL_FOR_PRIMITIVES failure does not indicate field name in exception message Sep 4, 2019
@cowtowncoder
Copy link
Member

@neetkee Agreed. At this point it would be necessary to find someone with time to work on this, however.

@WellingR
Copy link
Contributor

I also encountered this issue and wanted to report this. I have a potential fix ready and will create a PR soon

WellingR added a commit to WellingR/jackson-databind that referenced this issue Dec 27, 2019
cowtowncoder pushed a commit that referenced this issue Dec 30, 2019
@cowtowncoder cowtowncoder modified the milestones: 2.10.1, 2.10.2 Dec 30, 2019
cowtowncoder added a commit that referenced this issue Dec 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants