Skip to content

Commit 365cee6

Browse files
authored
Fix regression #4639 (#4738)
1 parent cc05d34 commit 365cee6

File tree

4 files changed

+77
-1
lines changed

4 files changed

+77
-1
lines changed

release-notes/VERSION-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ Project: jackson-databind
99
#4508: Deserialized JsonAnySetter field in Kotlin data class is null
1010
(reported by @MaximValeev)
1111
(fix by Joo-Hyuk K)
12+
#4639: @JsonAnySetter on field ignoring unrecognized properties if they are
13+
declared before the last recognized properties in JSON
14+
(reported by Sim Y-T)
15+
(fix by Joo-Hyuk K)
1216
#4718: Should not fail on trying to serialize `java.time.DateTimeException`
1317
#4724: Deserialization behavior change with Records, `@JsonCreator` and
1418
`@JsonValue` between 2.17 and 2.18

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,12 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
505505
// "any property"?
506506
if (_anySetter != null) {
507507
try {
508-
buffer.bufferAnyParameterProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
508+
// [databind#4639] Since 2.18.1 AnySetter might not part of the creator, but just some field.
509+
if (_anySetter.isFieldType()) {
510+
buffer.bufferAnyProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
511+
} else {
512+
buffer.bufferAnyParameterProperty(_anySetter, propName, _anySetter.deserialize(p, ctxt));
513+
}
509514
} catch (Exception e) {
510515
wrapAndThrow(e, _beanType.getRawClass(), propName, ctxt);
511516
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,13 @@ Object readResolve() {
194194
*/
195195
public int getParameterIndex() { return -1; }
196196

197+
/**
198+
* Method called to check whether this property is field
199+
*
200+
* @since 2.18.1
201+
*/
202+
public boolean isFieldType() { return _setterIsField; }
203+
197204
/**
198205
* Create an instance of value to pass through Creator parameter.
199206
*
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.fasterxml.jackson.databind.deser;
2+
3+
import java.util.Map;
4+
5+
import com.fasterxml.jackson.annotation.JsonAnySetter;
6+
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.databind.testutil.DatabindTestUtil;
9+
10+
import org.junit.jupiter.api.Test;
11+
12+
import static org.junit.jupiter.api.Assertions.assertEquals;
13+
14+
// [databind#4639] 2.18.1 : regression when using @JsonAnySetter outside of @JsonCreator
15+
public class JsonAnySetterFieldWithCreatorDeser4639Test
16+
extends DatabindTestUtil
17+
{
18+
19+
public static class Bean {
20+
private int b;
21+
private int d;
22+
23+
@JsonAnySetter
24+
private Map<String, ?> any;
25+
26+
@JsonCreator
27+
public Bean(@JsonProperty("b") int b, @JsonProperty("d") int d) {
28+
this.b = b;
29+
this.d = d;
30+
}
31+
}
32+
33+
@Test
34+
public void testJsonAnySetter()
35+
throws Exception
36+
{
37+
String json = "{\"a\":1,\"b\":2,\"c\":3,\"d\":4,\"e\":5,\"f\":6}";
38+
39+
Bean bean = newJsonMapper().readValue(json, Bean.class);
40+
assertEquals(2, bean.b);
41+
assertEquals(4, bean.d);
42+
43+
// failed with:
44+
// org.opentest4j.AssertionFailedError:
45+
// Expected :{b=2, c=3, e=5, f=6}
46+
// Actual :{e=5, f=6}
47+
assertEquals(mapOf("a", 1, "c", 3, "e", 5, "f", 6), bean.any);
48+
}
49+
50+
private Map<String, Integer> mapOf(String a, int i, String b, int i1, String c, int i2, String d, int i3)
51+
{
52+
Map<String, Integer> map = new java.util.HashMap<>();
53+
map.put(a, i);
54+
map.put(b, i1);
55+
map.put(c, i2);
56+
map.put(d, i3);
57+
return map;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)