If in json string, the property is missing, and the corresponding Java type 
is Optional, the deserialized field value is null, where Optional.Empty is 
expected.

There is the code to reproduce this issue.

// I know that using Optional type in class field is generally regarded as 
an anti-pattern.


    public static class FooRequest {
        private Optional<Long> foo;

        public Optional<Long> getFoo() {
            return foo;
        }

        public void setFoo(Optional<Long> foo) {
            this.foo = foo;
        }
    }


    @Test
    public void testDeserializeOptional_property_missing() throws Exception 
{
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new Jdk8Module());
        FooRequest fooRequest = mapper.readValue("{}", FooRequest.class);
*        // the issue : fooRequest.foo is null here, where Optional.Empty 
is expected*
        Assert.assertFalse(fooRequest.getFoo().isPresent());
    }

    @Test
    public void testDeserializeOptional_property_isnull() throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        mapper.registerModule(new Jdk8Module());
        FooRequest fooRequest = mapper.readValue("{\"foo\":null}", 
FooRequest.class);
        Assert.assertFalse(fooRequest.getFoo().isPresent());
    }

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/jackson-user/97734ab4-cebf-4646-a90d-aaf45960a2dao%40googlegroups.com.

Reply via email to