jackson-databind: Inconsistent behaviour with nulls for records
Forbidding nulls works correctly with classes but doesn’t with records. Moreover, the behaviour with records changes if we use @JsonProperty.
Let’s take the following ObjectMapper definition:
ObjectMapper objectMapper = JsonMapper.builder()
.defaultSetterInfo(JsonSetter.Value.construct(Nulls.FAIL, Nulls.FAIL))
.withCoercionConfigDefaults(config -> config.setCoercion(CoercionInputShape.String, CoercionAction.Fail))
.build();
When testing nulls with a class everything works as expected:
public class Test {
private String fieldName;
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
}
objectMapper.readValue("{ \"fieldName\": \"value\" }", Test.class); // ok (no exception is thrown)
objectMapper.readValue("{ \"fieldName\": null }", Test.class); // ok, throws InvalidNullException as expected
objectMapper.readValue("{ }", Test.class); // ok (no exception is thrown)
When testing the same class but adding @JsonProperty to it everything still works as expected:
public class Test {
private @JsonProperty("field_name") String fieldName;
public void setFieldName(String fieldName) {
this.fieldName = fieldName;
}
}
objectMapper.readValue("{ \"field_name\": \"value\" }", Test.class); // ok (no exception is thrown)
objectMapper.readValue("{ \"field_name\": null }", Test.class); // ok, throws InvalidNullException as expected
objectMapper.readValue("{ }", Test.class); // ok (no exception is thrown)
When testing nulls with a record null hating doesn’t seem to be working:
public record Test(String fieldName) {}
objectMapper.readValue("{ \"fieldName\": \"value\" }", Test.class); // ok (no exception is thrown)
objectMapper.readValue("{ \"fieldName\": null }", Test.class); // not ok, was expecting a InvalidNullException to be thrown but it wasn't
objectMapper.readValue("{ }", Test.class); // ok (no exception is thrown)
When testing the same record but adding @JsonProperty to it null hating starts working but something else breaks:
public record Test(@JsonProperty("field_name") String fieldName) {}
objectMapper.readValue("{ \"field_name\": \"value\" }", Test.class); // ok (no exception is thrown)
objectMapper.readValue("{ \"field_name\": null }", Test.class); // ok, throws InvalidNullException as expected
objectMapper.readValue("{ }", Test.class); // not ok, throws InvalidNullException when it shouldn't
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 16 (11 by maintainers)
@joca-bt From the attached project, I see that you’re using version
2.14.2. I think this issue has been fixed in2.15.0by #3724, which is why #3848 wasn’t able to reproduce the issue.You can verify this by testing with com.fasterxml.jackson.core:jackson-databind:2.15.0-rc2 (don’t forget to also upgrade any other Jackson dependency to the same version, to avoid any unnecessary problems).
We tried updating to 2.18 and had an outage, so I guess there were some breaking changes (which can be actual fixes - we didn’t have time to look and write tests).
Just to repeat myself, a Class equivalent of Record is not one with setter, but this:
(There were some behavioural discrepancies between a Class with constructor creator vs Records, but they are now mostly aligned in
2.15.0.)…which will have this behaviour:
Knowing that, this issue is really about the behavioural difference between a POJO with constructor creator vs setter, i.e. #3870.
You can inspect the zip without running it if you have security concerns.
Best, João
On Wed, 29 Mar 2023, 04:35 Kim, Joo Hyuk, @.***> wrote: