question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

custom deserialization with absent Optional returns null

See original GitHub issue

When using a custom deserialization for an Optional field, then the absence of that field is returning null instead of Optional.empty().

I reproduced with my own test method in the test class OptionalTest.java:

    public void testWithCustomDeserializerIfOptionalAbsent() throws Exception
    {
        assertEquals(Optional.empty(), MAPPER.readValue("{}",
                CaseChangingStringWrapper.class).value);

        assertEquals(Optional.empty(), MAPPER.readValue(aposToQuotes("{'value':null}"),
                CaseChangingStringWrapper.class).value);
    }

The first check fails, because of Jackson setting the value field to null.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
rkeytackedcommented, Aug 12, 2017

Finally I found the root cause of my misunderstanding. The deserialization was inconsistent amongst my POJOs.

I just realized that this is because of Lombok generating the @ConstructorProperties to the @AllArgsConstructor. Lombok does that even if the constructor is implicit by @Builder, and even if access level is set to PRIVATE explicitely.

Jackson calls this annotated constructor always with Optional.empty() values for all absent fields that are of type Optional. Even if the constructor is private. Hence my POJOs behave differently when defined with or without Lombok’s @AllArgsConstructor.

Similar issues have been discussed last year already, e.g. https://github.com/FasterXML/jackson-databind/issues/618 Not sure, if someone actually realized before that the different behavior for Optional fields comes from Lombok’s generated @ConstructorProperties.

0reactions
cowtowncodercommented, Aug 14, 2017

Yes, there is special tracking for members coming via creators, since they must be buffered anyway (unlike field/setter assigned ones). There is also an issue (quite old) for trying to extend optional handling of missing values to all properties. Its implementation has not started, there is some hope it could be done with Jackson 3.x.

So I think it can be considered an implementation limitation at this point.

Will close.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jackson deserializes absent optionals as nulls instead of ...
I've noticed that deserializing a json string to this object ... As you can notice the optional parameter is absent from this json...
Read more >
Using Optional with Jackson - Baeldung
In this case, isPresent() is a public getter on the Optional class. This means it will be serialized with a value of true...
Read more >
Jackson - Ignoring Null, Empty and Absent Values
Learn to serialize the fields and ignore NULL, empty and absent values using Jackson. In this tutorial, we will learn the difference between...
Read more >
Java 8 Optional Class - GeeksforGeeks
Additional methods that depend on the presence or absence of a contained value are provided, such as orElse() which returns a default value...
Read more >
DeserializationFeature (jackson-databind 2.9.0 API) - FasterXML
If disabled, and if property is NOT marked as required, missing Creator properties are filled with null values provided by deserializer for the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found