Skip to content

Commit e40aaee

Browse files
authored
Fix #2692 (#5256)
1 parent ff8c5a2 commit e40aaee

File tree

7 files changed

+66
-61
lines changed

7 files changed

+66
-61
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ Project: jackson-databind
66

77
Not yet released
88

9+
#2692: Should never call `set()` on setterless property during deserialization
10+
(reported by @lbonco)
911
#5237: Failing `@JsonMerge` with a custom Map with a `@JsonCreator` constructor
1012
(reported by @nlisker)
1113
#5242: Support "binary vectors": `@JsonFormat(shape = Shape.BINARY)` for

src/main/java/com/fasterxml/jackson/databind/deser/BeanDeserializer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
480480
// 12-Aug-2025, tatu: [databind#5237] Mergeable properties need
481481
// special handling: must defer deserialization until POJO
482482
// is constructed.
483-
if (prop instanceof MergingSettableBeanProperty) {
483+
if (prop.isMerging()) {
484484
TokenBuffer tb = ctxt.bufferForInputBuffering(p);
485485
tb.copyCurrentStructure(p);
486486
buffer.bufferMergingProperty(prop, tb);

src/main/java/com/fasterxml/jackson/databind/deser/SettableBeanProperty.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,16 @@ public void markAsIgnorable() { /* nop */ }
342342
*/
343343
public boolean isIgnorable() { return false; }
344344

345+
/**
346+
* Whether this property requires merging of values (read-then-write)
347+
*
348+
* @since 2.20
349+
*/
350+
public boolean isMerging() {
351+
// Most are not merging so default to this implementation
352+
return false;
353+
}
354+
345355
/*
346356
/**********************************************************
347357
/* BeanProperty impl

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ protected SettableBeanProperty withDelegate(SettableBeanProperty d) {
6060
return new MergingSettableBeanProperty(d, _accessor);
6161
}
6262

63+
@Override // since 2.20
64+
public boolean isMerging() {
65+
return true;
66+
}
67+
6368
/*
6469
/**********************************************************
6570
/* Deserialization methods

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,11 @@ public void fixAccess(DeserializationConfig config) {
7979
config.isEnabled(MapperFeature.OVERRIDE_PUBLIC_ACCESS_MODIFIERS));
8080
}
8181

82+
@Override // since 2.20
83+
public boolean isMerging() {
84+
return true;
85+
}
86+
8287
/*
8388
/**********************************************************
8489
/* BeanProperty impl

src/test/java/com/fasterxml/jackson/databind/deser/SetterlessPropertiesDeserTest.java

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,17 @@
88

99
import com.fasterxml.jackson.databind.*;
1010
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
11+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
1112

1213
import static org.junit.jupiter.api.Assertions.*;
1314

14-
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.jsonMapperBuilder;
15-
import static com.fasterxml.jackson.databind.testutil.DatabindTestUtil.verifyException;
16-
1715
/**
18-
* Unit tests for verifying that feature requested
19-
* via [JACKSON-88] ("setterless collections") work as
16+
* Unit tests for verifying that "setterless collections" work as
2017
* expected, similar to how Collections and Maps work
2118
* with JAXB.
2219
*/
2320
public class SetterlessPropertiesDeserTest
21+
extends DatabindTestUtil
2422
{
2523
static class CollectionBean
2624
{
@@ -48,17 +46,43 @@ public List<Integer> getList() {
4846
}
4947
}
5048

49+
static class DataBean2692
50+
{
51+
final String val;
52+
53+
@JsonCreator
54+
public DataBean2692(@JsonProperty(value = "val") String val) {
55+
super();
56+
this.val = val;
57+
}
58+
59+
public String getVal() {
60+
return val;
61+
}
62+
63+
public List<String> getList() {
64+
return new ArrayList<>();
65+
}
66+
67+
@Override
68+
public String toString() {
69+
return "DataBean [val=" + val + "]";
70+
}
71+
}
72+
5173
/*
5274
/**********************************************************
5375
/* Unit tests
5476
/**********************************************************
5577
*/
5678

79+
private final ObjectMapper MAPPER = newJsonMapper();
80+
5781
@Test
5882
public void testSimpleSetterlessCollectionOk()
5983
throws Exception
6084
{
61-
CollectionBean result = new ObjectMapper().readValue
85+
CollectionBean result = MAPPER.readValue
6286
("{\"values\":[ \"abc\", \"def\" ]}", CollectionBean.class);
6387
List<String> l = result._values;
6488
assertEquals(2, l.size());
@@ -74,10 +98,9 @@ public void testSimpleSetterlessCollectionOk()
7498
public void testSimpleSetterlessCollectionFailure()
7599
throws Exception
76100
{
77-
ObjectMapper m = new ObjectMapper();
78101
// by default, it should be enabled
79-
assertTrue(m.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
80-
m = jsonMapperBuilder()
102+
assertTrue(MAPPER.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
103+
ObjectMapper m = jsonMapperBuilder()
81104
.configure(MapperFeature.USE_GETTERS_AS_SETTERS, false)
82105
.build();
83106
assertFalse(m.isEnabled(MapperFeature.USE_GETTERS_AS_SETTERS));
@@ -98,7 +121,7 @@ public void testSimpleSetterlessCollectionFailure()
98121
public void testSimpleSetterlessMapOk()
99122
throws Exception
100123
{
101-
MapBean result = new ObjectMapper().readValue
124+
MapBean result = MAPPER.readValue
102125
("{\"values\":{ \"a\": 15, \"b\" : -3 }}", MapBean.class);
103126
Map<String,Integer> m = result._values;
104127
assertEquals(2, m.size());
@@ -136,4 +159,14 @@ public void testSetterlessPrecedence() throws Exception
136159
assertNotNull(value);
137160
assertEquals(3, value.values.size());
138161
}
162+
163+
// [databind#2692]
164+
@Test
165+
void issue2692() throws Exception {
166+
ObjectMapper om = newJsonMapper();
167+
String json = "{\"list\":[\"11\"],\"val\":\"VAL2\"}";
168+
DataBean2692 out = om.readerFor(DataBean2692.class).readValue(json);
169+
// System.out.println("this is ko" + out);
170+
assertNotNull(out);
171+
}
139172
}

src/test/java/com/fasterxml/jackson/databind/tofix/SetterlessList2692Test.java

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)