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

Incorrect target type for arrays when providing nulls and nulls are disabled #2567

Closed
joca-bt opened this issue Dec 9, 2019 · 1 comment
Closed
Milestone

Comments

@joca-bt
Copy link

joca-bt commented Dec 9, 2019

In the following situation the target type of a MismatchedInputException is incorrect.

I have an ObjectMapper without scalar conversion and that doesn't allow nulls.

try {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
        .setDefaultSetterInfo(JsonSetter.Value.construct(Nulls.FAIL, Nulls.FAIL));
    objectMapper.readValue("{ \"array\": [ null ]}", Foo.class);
} catch (MismatchedInputException exception) {
    JsonParser parser = (JsonParser) exception.getProcessor();
    System.out.println(exception);
    System.out.println("target type: " + exception.getTargetType());
    System.out.println("current token: " + parser.currentToken());
    for (var node : exception.getPath()) {
        System.out.println("node: " + node);
        System.out.println("field name: " + node.getFieldName());
        System.out.println("index: " + node.getIndex());
    }
}
public static class Foo {
    public List<Boolean> array;
}

When I provide an integer as the element of the array of booleans I get that the target type was boolean (correct, since it is an array of booleans).

com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot coerce Number (1) for type `java.lang.Boolean` (enable `MapperFeature.ALLOW_COERCION_OF_SCALARS` to allow)
 at [Source: (String)"{ "array": [ 1 ]}"; line: 1, column: 14] (through reference chain: com.booking.demandapi.system.Initializer$Foo["array"]->java.util.ArrayList[0])
target type: class java.lang.Boolean
current token: VALUE_NUMBER_INT
node: com.booking.demandapi.system.Initializer$Foo["array"]
field name: array
index: -1
node: java.util.ArrayList[0]
field name: null
index: 0

However, when I provide a null as the element I get that the target type was array. I was expecting the target type to be boolean as before.

com.fasterxml.jackson.databind.exc.InvalidNullException: Invalid `null` value encountered for property "array"
 at [Source: (String)"{ "array": [ null ]}"; line: 1, column: 14] (through reference chain: com.booking.demandapi.system.Initializer$Foo["array"]->java.util.ArrayList[0])
target type: interface java.util.List
current token: VALUE_NULL
node: com.booking.demandapi.system.Initializer$Foo["array"]
field name: array
index: -1
node: java.util.ArrayList[0]
field name: null
index: 0
@cowtowncoder
Copy link
Member

Yes, it looks like "target type" should refer to Boolean, not List, even thought it is List deserializer that handles the problem. I'll see how easy this would be to fix.

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

2 participants