Skip to content

Commit 2d67b15

Browse files
authored
Fix @JsonAnySetter issue with "setter" method (related to #4639) (#4790)
1 parent 764c83b commit 2d67b15

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

release-notes/VERSION-2.x

+11-7
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,12 @@ Project: jackson-databind
1313
#4788: `EnumFeature.WRITE_ENUMS_TO_LOWERCASE` overrides `@JsonProperty` values
1414
(reported by Mike M)
1515
(fix by Joo-Hyuk K)
16+
#4790: Fix `@JsonAnySetter` issue with "setter" method (related to #4639)
17+
(reported by @bsa01)
18+
(fix by Joo-Hyuk K)
1619

1720
2.18.1 (28-Oct-2024)
1821

19-
#4741: When `Include.NON_DEFAULT` setting is used on POJO, empty values
20-
are not included in json if default is `null`
21-
(reported by @ragnhov)
22-
(fix by Joo-Hyuk K)
23-
#4749: Fixed a problem with `StdDelegatingSerializer#serializeWithType` looking up the serializer
24-
with the wrong argument
25-
(fix by wrongwrong)
2622
#4508: Deserialized JsonAnySetter field in Kotlin data class is null
2723
(reported by @MaximValeev)
2824
(fix by Joo-Hyuk K)
@@ -34,6 +30,14 @@ Project: jackson-databind
3430
#4724: Deserialization behavior change with Records, `@JsonCreator` and
3531
`@JsonValue` between 2.17 and 2.18
3632
(reported by Antti L)
33+
#4727: Eclipse having issues due`module-info` class "lost" on 2.18.0 jars
34+
#4741: When `Include.NON_DEFAULT` setting is used on POJO, empty values
35+
are not included in json if default is `null`
36+
(reported by @ragnhov)
37+
(fix by Joo-Hyuk K)
38+
#4749: Fixed a problem with `StdDelegatingSerializer#serializeWithType` looking up the serializer
39+
with the wrong argument
40+
(fix by wrongwrong)
3741

3842
2.18.0 (26-Sep-2024)
3943

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,9 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
506506
if (_anySetter != null) {
507507
try {
508508
// [databind#4639] Since 2.18.1 AnySetter might not part of the creator, but just some field.
509-
if (_anySetter.isFieldType()) {
509+
if (_anySetter.isFieldType() ||
510+
// [databind#4639] 2.18.2: Also should account for setter type :-/
511+
_anySetter.isSetterType()) {
510512
buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
511513
} else {
512514
buffer.bufferAnyParameterProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));

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

+7
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ Object readResolve() {
201201
*/
202202
public boolean isFieldType() { return _setterIsField; }
203203

204+
/**
205+
* Method called to check whether this property is method
206+
*
207+
* @return 2.18.2
208+
*/
209+
public boolean isSetterType() { return _setter instanceof AnnotatedMethod; }
210+
204211
/**
205212
* Create an instance of value to pass through Creator parameter.
206213
*

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

+41-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.fasterxml.jackson.databind.deser;
22

3+
import java.util.HashMap;
34
import java.util.Map;
45

6+
import org.junit.jupiter.api.Test;
7+
58
import com.fasterxml.jackson.annotation.JsonAnySetter;
69
import com.fasterxml.jackson.annotation.JsonCreator;
710
import com.fasterxml.jackson.annotation.JsonProperty;
11+
import com.fasterxml.jackson.databind.ObjectMapper;
812
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
913

10-
import org.junit.jupiter.api.Test;
11-
1214
import static org.junit.jupiter.api.Assertions.assertEquals;
1315

1416
// [databind#4639] 2.18.1 : regression when using @JsonAnySetter outside of @JsonCreator
@@ -30,13 +32,49 @@ public Bean(@JsonProperty("b") int b, @JsonProperty("d") int d) {
3032
}
3133
}
3234

35+
public static class SomeBean {
36+
final int b;
37+
final int d;
38+
final Map<String, Object> any = new HashMap<>();
39+
40+
@JsonCreator
41+
public SomeBean(@JsonProperty("b") int b, @JsonProperty("d") int d) {
42+
this.b = b;
43+
this.d = d;
44+
}
45+
46+
@JsonAnySetter
47+
public void setAny(String name, Object value) {
48+
any.put(name, value);
49+
}
50+
}
51+
52+
final ObjectMapper MAPPER = newJsonMapper();
53+
3354
@Test
3455
public void testJsonAnySetter()
3556
throws Exception
3657
{
3758
String json = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5,\"f\":6}";
3859

39-
Bean bean = newJsonMapper().readValue(json, Bean.class);
60+
Bean bean = MAPPER.readValue(json, Bean.class);
61+
assertEquals(2, bean.b);
62+
assertEquals(4, bean.d);
63+
64+
// failed with:
65+
// org.opentest4j.AssertionFailedError:
66+
// Expected :{b=2, c=3, e=5, f=6}
67+
// Actual :{e=5, f=6}
68+
assertEquals(mapOf("a", 1, "c", 3, "e", 5, "f", 6), bean.any);
69+
}
70+
71+
@Test
72+
public void testJsonAnySetterWithField()
73+
throws Exception
74+
{
75+
String json = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5,\"f\":6}";
76+
77+
SomeBean bean = MAPPER.readValue(json, SomeBean.class);
4078
assertEquals(2, bean.b);
4179
assertEquals(4, bean.d);
4280

0 commit comments

Comments
 (0)