jackson-databind-nullable
jackson-databind-nullable copied to clipboard
Deserialising an empty character as json null instead of json undefined
Recently we changed a type in one object from JsonNullable<String> sth = JsonNullable.undefined() to JsonNullable<Character> sth = JsonNullable.undefined(). Soon after we noticed a change in the deserialisation behaviour - explicit updates of that property to an empty string ("") no longer worked, as they were treated as not present (JsonNullable.undefined instead of JsonNullable[null]).
It seems that the issue is in the JsonNullableDeserializer, namely in the constructor:
this.isStringDeserializer = ((ReferenceType) fullType).getReferencedType().isTypeOrSubTypeOf(String.class);
Does it make sense to change the check and include Character as well as String? Or would you suggest another alternative? If it is the former, we can also open a PR, as the change doesn't seem so large (and there is already https://github.com/OpenAPITools/jackson-databind-nullable/pull/4 for inspiration).
I think the Problem is, that a Character can only be set or null, there is nothing like an empty value. Furthermore, there is no such json type like a single character. JSON only knows about Boolean, Numbers, Strings, Arrays, Lists and an object consiting of these. A single character is basically just a number. When you deserialize a string value to a character via jackson, it's some kind of implicit / convenience conversion and just works although it's not really defined by the json standard.
I think what you would like to achieve can only be made by using JsonNullable<Integer> with:
- 0 == empty()
- null = undefined()
- any other
If you need an char, you can just cast the integer prior using it.
After thinking about this issue, I think it's best to align the deserialization behaviour for String and Character to make it more consistent. See PR #45.
@milanov can you please test the fix filed by @bratkartoffel to see if it works for you?
https://github.com/OpenAPITools/jackson-databind-nullable/pull/45