Whitespace string is considered `null` when deserializing to primitive
See original GitHub issueDescribe the bug
Setting whitespace string to an int
property while ALLOW_COERCION_OF_SCALARS
and ACCEPT_EMPTY_STRING_AS_NULL_OBJECT
are disabled, results in Cannot coerce null to int
error, while empty string is considered an actual string in that case.
This could potentially become a problem if int
was changed to Integer
. In that case json will be successfully parsed for whitespace string and fail for empty.
Might be related to https://github.com/FasterXML/jackson-databind/issues/3234
Version information 2.13.3
To Reproduce Mapper builder
new Jackson2ObjectMapperBuilder()
.featuresToDisable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
.featuresToDisable(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)
.featuresToDisable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
.featuresToDisable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
.featuresToEnable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
.featuresToEnable(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES);
Test code
test("\"\"");
test("\" \"");
test("\" \"");
test("\"\\n\"");
void test(String value) throws Exception {
System.out.println("{\"value\":" + value + "}");
try {
Model model = OM.readValue("{\"value\":" + value + "}", Model.class);
} catch (JsonMappingException e) {
System.out.println(e.getLocalizedMessage());
}
}
public class Model {
private int value;
public Model() {
}
public void setValue(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}
Output
{"value":""}
Cannot coerce empty String ("") to `int` value (but could if coercion was enabled using `CoercionConfig`)
at [Source: (String)"{"value":""}"; line: 1, column: 10] (through reference chain: test.MainTest$Model["value"])
{"value":" "}
Cannot coerce `null` to `int` value (disable `DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES` to allow)
at [Source: (String)"{"value":" "}"; line: 1, column: 10] (through reference chain: test.MainTest$Model["value"])
{"value":" "}
Cannot coerce `null` to `int` value (disable `DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES` to allow)
at [Source: (String)"{"value":" "}"; line: 1, column: 10] (through reference chain: test.MainTest$Model["value"])
{"value":"\n"}
Cannot coerce `null` to `int` value (disable `DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES` to allow)
at [Source: (String)"{"value":"\n"}"; line: 1, column: 10] (through reference chain: test.MainTest$Model["value"])
Expected behavior Whitespace string should be handled as any other string.
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Deserialize Json issue due to spaces - Stack Overflow
Therefore my Item which has properties of FirstName and SecondName (see below) will get a null value. public class Item { private string...
Read more >Use empty string, null or remove empty property in API request ...
TLDR; Remove null properties. The first thing to bear in mind is that applications at their edges are not object-oriented (nor functional if ......
Read more >XML Schema Part 2: Datatypes Second Edition - W3C
Any string compatible with the RFC can occur in an element or attribute validated by this type, because the ·whiteSpace· facet of this...
Read more >JSON.stringify() - JavaScript - MDN Web Docs
If space is anything other than a string or number (can be either a primitive or a wrapper object) — for example, is...
Read more >Migrate from Newtonsoft.Json to System.Text.Json - .NET
Json deserializes to Object, it: Infers the type of primitive values in the JSON payload (other than null ) and returns the stored...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@bigboynaruto I don’t know where
Jackson2ObjectMapperBuilder
comes from, but since it’s not part of databind it might not have access to this configuration. However:ObjectMapper.copy()
should copy these settings, so if that is being used, coercion configs should come along. If not that’d be a bug, and we’d appreciate a simple test case to show this (there are some tests forObjectMapper.copy()
to try to find other problems, but not yet (I think) for coercion configs).Jackson 3.0 (from
master
) has much better way to deal with builders, but alas it’s still some way off.It’s caused by this code: https://github.com/FasterXML/jackson-databind/blob/c100ed5b3bbb2ae7d1287ae368672469a1bafd0c/src/main/java/com/fasterxml/jackson/databind/cfg/CoercionConfigs.java#L310-L314
Looks like it is necessary for compatibility, however specifying an explicit coercion config for the type should work fine.