Incorrect target type for arrays when providing nulls and nulls are disabled (original) (raw)
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