-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional<JsonNode>
deserialization from "absent" value does not work in the expected way
#250
Comments
Hmmmh. I think you are right in that handling of |
@mloho I implemented #251 for upcoming 2.14. It will allow changing behavior; however, in that case value becomes Java It is frustratingly difficult to figure out behavior that everyone would find intuitive here... |
Optional<JsonNode>
deserialization from "absent" value does not work in the expected way
@cowtowncoder, agreed! Configurability typically covers most if not all use cases (depending on the extensiveness of the config / complexity of the domain). However, for default intuitiveness, I always try to refer to the documentation.. I'm not sure if the JLS states best practices for Optionals, but if I try to return a In any case, (in my opinion) having an My @Override
public Object getAbsentValue(DeserializationContext ctxt) throws JsonMappingException {
return Optional.ofNullable(_valueDeserializer.getAbsentValue(ctxt));
} |
@mloho Problem is that this is a behavior change that affects all users with existing usage, making it much less desireable to change the default behavior. Even if I agreed your choice is better default on its own, the breaking chance (for some users) may outweigh benefits of change. But wrt configuration we have 2 aspects to consider:
I am guessing all 3 choices would have proponents:
The question, then, is:
On Configurability we could probably go with 2 features (even if only 3 out of 4 combinations are usable):
and I would think that for backwards-compatibility reason we would probably want both to be enabled by default. If above makes sense, would you mind filing a request to |
Example:
When deserialized from:
{}
Expected:
myField.isPresent() == false
Actual:
myField.isPresent() == true
This is because
myField
gets set to anOptional
of aNullNode
After spending some time looking into the source code of both the
jackson-databind
and thejackson-datatype-jdk8
libraries, the problem seems to lie in theOptionalDeserializer
(or higher).During deserialization, when a property is missing, the
PropertyValueBuffer::_findMissing
method is called and in it, this piece of code is called:https://github.com/FasterXML/jackson-databind/blob/0fe97e0d69b7d5362907b094d5b979bc2216dc4a/src/main/java/com/fasterxml/jackson/databind/deser/impl/PropertyValueBuffer.java#L203
The
OptionalDeserializer
is not overriding its inheritedgetAbsentValue
method to returnOptional.ofNullable(_valueDeserializer.getAbsentValue(ctxt));
(or similar).Due to the lack of the overriding, the inherited
getAbsentValue
method actually callsgetNullValue
instead as can be seen here:https://github.com/FasterXML/jackson-databind/blob/0fe97e0d69b7d5362907b094d5b979bc2216dc4a/src/main/java/com/fasterxml/jackson/databind/JsonDeserializer.java#L349
In the case of a
JsonNode
, theJsonNodeDeserializer
is used. This deserializer overrides thegetNullValue
method to return aNullNode
.https://github.com/FasterXML/jackson-databind/blob/0fe97e0d69b7d5362907b094d5b979bc2216dc4a/src/main/java/com/fasterxml/jackson/databind/deser/std/JsonNodeDeserializer.java#L73
The text was updated successfully, but these errors were encountered: