Skip to content

Commit 04ead2e

Browse files
committed
Fix #1437
1 parent b5cb96c commit 04ead2e

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/impl/MergingSettableBeanProperty.java

+28-9
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@
1010
/**
1111
* {@link SettableBeanProperty} implementation that will try to access value of
1212
* the property first, and if non-null value found, pass that for update
13-
* (using {@link com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext, Object)}
14-
* ) instead of constructing a new value. This is necessary to support "merging" properties.
13+
* (using {@link com.fasterxml.jackson.databind.JsonDeserializer#deserialize(com.fasterxml.jackson.core.JsonParser, com.fasterxml.jackson.databind.DeserializationContext, Object)})
14+
* instead of constructing a new value. This is necessary to support "merging" properties.
15+
*<p>
16+
* Note that there are many similarities to {@link SetterlessProperty}, which predates
17+
* this variant; and that one is even used in cases where there is no mutator
18+
* available.
1519
*
1620
* @since 2.9
1721
*/
@@ -76,7 +80,11 @@ public void deserializeAndSet(JsonParser p, DeserializationContext ctxt,
7680
newValue = delegate.deserializeWith(p, ctxt, oldValue);
7781
}
7882
if (newValue != oldValue) {
79-
delegate.set(instance, newValue);
83+
// 31-Oct-2016, tatu: Basically should just ignore as null can't really
84+
// contribute to merging.
85+
if (newValue != null) {
86+
delegate.set(instance, newValue);
87+
}
8088
}
8189
}
8290

@@ -97,20 +105,31 @@ public Object deserializeSetAndReturn(JsonParser p,
97105
// try calling setter on builder? Presumably should not be required,
98106
// but may need to revise
99107
if (newValue != oldValue) {
100-
return delegate.setAndReturn(instance, newValue);
108+
// 31-Oct-2016, tatu: Basically should just ignore as null can't really
109+
// contribute to merging.
110+
if (newValue != null) {
111+
return delegate.setAndReturn(instance, newValue);
112+
}
101113
}
102114
return instance;
103115
}
104116

105117
@Override
106118
public void set(Object instance, Object value) throws IOException {
107-
delegate.set(instance, value);
119+
// 31-Oct-2016, tatu: Basically should just ignore as null can't really
120+
// contribute to merging.
121+
if (value != null) {
122+
delegate.set(instance, value);
123+
}
108124
}
109125

110126
@Override
111-
public Object setAndReturn(Object instance, Object value)
112-
throws IOException
113-
{
114-
return delegate.setAndReturn(instance, value);
127+
public Object setAndReturn(Object instance, Object value) throws IOException {
128+
// 31-Oct-2016, tatu: Basically should just ignore as null can't really
129+
// contribute to merging.
130+
if (value != null) {
131+
return delegate.setAndReturn(instance, value);
132+
}
133+
return instance;
115134
}
116135
}

src/main/java/com/fasterxml/jackson/databind/deser/impl/SetterlessProperty.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,6 @@ public final void set(Object instance, Object value) throws IOException {
145145
public Object setAndReturn(Object instance, Object value) throws IOException
146146
{
147147
set(instance, value);
148-
return null;
148+
return instance;
149149
}
150150
}

src/test/java/com/fasterxml/jackson/failing/MergeWithNullTest.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/merge/MergeWithNullTest.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.failing;
1+
package com.fasterxml.jackson.databind.deser.merge;
22

33
import com.fasterxml.jackson.annotation.JsonSetter;
44
import com.fasterxml.jackson.annotation.OptBoolean;
@@ -19,6 +19,14 @@ public Config(int a, int b) {
1919
}
2020
}
2121

22+
// another variant where all we got is a getter
23+
static class NoSetterConfig {
24+
AB _value = new AB(2, 3);
25+
26+
@JsonSetter(merge=OptBoolean.TRUE)
27+
public AB getValue() { return _value; }
28+
}
29+
2230
static class AB {
2331
public int a;
2432
public int b;
@@ -29,7 +37,7 @@ public AB(int a0, int b0) {
2937
b = b0;
3038
}
3139
}
32-
40+
3341
private final ObjectMapper MAPPER = new ObjectMapper()
3442
// 26-Oct-2016, tatu: Make sure we'll report merge problems by default
3543
.disable(MapperFeature.IGNORE_MERGE_FOR_UNMERGEABLE)
@@ -44,6 +52,15 @@ public void testBeanMergingWithNull() throws Exception
4452
assertEquals(5, config.loc.a);
4553
assertEquals(7, config.loc.b);
4654
}
47-
4855

56+
public void testSetterlessMergingWithNull() throws Exception
57+
{
58+
NoSetterConfig input = new NoSetterConfig();
59+
NoSetterConfig result = MAPPER.readerForUpdating(input)
60+
.readValue(aposToQuotes("{'value':null}"));
61+
assertNotNull(result.getValue());
62+
assertEquals(2, result.getValue().a);
63+
assertEquals(3, result.getValue().b);
64+
assertSame(input, result);
65+
}
4966
}

0 commit comments

Comments
 (0)