diff --git a/datatypes/src/main/java/com/fasterxml/jackson/datatype/jdk8/OptionalDeserializer.java b/datatypes/src/main/java/com/fasterxml/jackson/datatype/jdk8/OptionalDeserializer.java index 12c39f10..7440b1a5 100644 --- a/datatypes/src/main/java/com/fasterxml/jackson/datatype/jdk8/OptionalDeserializer.java +++ b/datatypes/src/main/java/com/fasterxml/jackson/datatype/jdk8/OptionalDeserializer.java @@ -60,7 +60,9 @@ public Optional referenceValue(Object contents) { @Override public Object getReferenced(Optional reference) { - return reference.get(); + // 23-Apr-2021, tatu: [modules-java8#214] Need to support empty + // for merging too + return reference.orElse(null); } @Override // since 2.9 diff --git a/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalMergeTest.java b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalMergeTest.java new file mode 100644 index 00000000..fc5eaddf --- /dev/null +++ b/datatypes/src/test/java/com/fasterxml/jackson/datatype/jdk8/OptionalMergeTest.java @@ -0,0 +1,33 @@ +package com.fasterxml.jackson.datatype.jdk8; + +import java.util.Arrays; +import java.util.List; +import java.util.Optional; + +import com.fasterxml.jackson.annotation.JsonMerge; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class OptionalMergeTest extends ModuleTestBase +{ + // [modules-java8#214] + static class OptionalListWrapper { + @JsonMerge + public Optional> list = Optional.empty(); + } + + private final ObjectMapper MAPPER = mapperWithModule(); + + // [modules-java8#214]: ReferenceType of List, merge + public void testMergeToListViaRef() throws Exception + { + OptionalListWrapper base = MAPPER.readValue(aposToQuotes("{'list':['a']}"), + OptionalListWrapper.class); + assertNotNull(base.list); + assertEquals(Arrays.asList("a"), base.list.get()); + + OptionalListWrapper merged = MAPPER.readerForUpdating(base) + .readValue(aposToQuotes("{'list':['b']}")); + assertSame(base, merged); + assertEquals(Arrays.asList("a", "b"), base.list.get()); + } +} diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 9e05b445..86fd2167 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -8,6 +8,12 @@ Modules: === Releases === ------------------------------------------------------------------------ +2.12.4 (not yet released) + +#214: readerForUpdating(objectToUpdate).readValue(json) behaves unexpectedly + on Optional + (reported by jc84-dev@github) + 2.12.3 (12-Apr-2021) #207: Fail to serialize `TemporalAdjuster` type with 2.12