Skip to content

Commit 0f8ed73

Browse files
committed
Fix #214
1 parent 39e86c0 commit 0f8ed73

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

datatypes/src/main/java/com/fasterxml/jackson/datatype/jdk8/OptionalDeserializer.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ public Optional<?> referenceValue(Object contents) {
6060

6161
@Override
6262
public Object getReferenced(Optional<?> reference) {
63-
return reference.get();
63+
// 23-Apr-2021, tatu: [modules-java8#214] Need to support empty
64+
// for merging too
65+
return reference.orElse(null);
6466
}
6567

6668
@Override // since 2.9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.fasterxml.jackson.datatype.jdk8;
2+
3+
import java.util.Arrays;
4+
import java.util.List;
5+
import java.util.Optional;
6+
7+
import com.fasterxml.jackson.annotation.JsonMerge;
8+
import com.fasterxml.jackson.databind.ObjectMapper;
9+
10+
public class OptionalMergeTest extends ModuleTestBase
11+
{
12+
// [modules-java8#214]
13+
static class OptionalListWrapper {
14+
@JsonMerge
15+
public Optional<List<String>> list = Optional.empty();
16+
}
17+
18+
private final ObjectMapper MAPPER = mapperWithModule();
19+
20+
// [modules-java8#214]: ReferenceType of List, merge
21+
public void testMergeToListViaRef() throws Exception
22+
{
23+
OptionalListWrapper base = MAPPER.readValue(aposToQuotes("{'list':['a']}"),
24+
OptionalListWrapper.class);
25+
assertNotNull(base.list);
26+
assertEquals(Arrays.asList("a"), base.list.get());
27+
28+
OptionalListWrapper merged = MAPPER.readerForUpdating(base)
29+
.readValue(aposToQuotes("{'list':['b']}"));
30+
assertSame(base, merged);
31+
assertEquals(Arrays.asList("a", "b"), base.list.get());
32+
}
33+
}

release-notes/VERSION-2.x

+6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ Modules:
88
=== Releases ===
99
------------------------------------------------------------------------
1010

11+
2.12.4 (not yet released)
12+
13+
#214: readerForUpdating(objectToUpdate).readValue(json) behaves unexpectedly
14+
on Optional<List>
15+
(reported by jc84-dev@github)
16+
1117
2.12.3 (12-Apr-2021)
1218

1319
#207: Fail to serialize `TemporalAdjuster` type with 2.12

0 commit comments

Comments
 (0)