Nulls.AS_EMPTY doesn't work for collections containing values without default constructor (like Records) (original) (raw)
Describe the bug
When deserializating an empty JSON object {} into a record that contains a list of another record results in:
java.lang.IllegalStateException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot create empty instance of [collection type; class java.util.List, contains [simple type, class com.example.Test$Foo$Bar]], no default Creator
at [Source: (String)"{}"; line: 1, column: 1]
Version information
2.12.2
To Reproduce
record Foo(List<Bar> list) {
record Bar(String name, String value) {}
}
@Test
public void test() {
ObjectMapper mapper = new ObjectMapper().setDefaultSetterInfo(JsonSetter.Value.forContentNulls(Nulls.AS_EMPTY));
try {
mapper.readValue("{}", Foo.class);
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
}
The above fails with:
java.lang.IllegalStateException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot create empty instance of [collection type; class java.util.List, contains [simple type, class com.example.Test$Foo$Bar]], no default Creator
at [Source: (String)"{}"; line: 1, column: 1]
Expected behavior
I would expect the deserialization give me an empty list.