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());
}