Skip to content

Commit 62a832e

Browse files
committed
dont track unknown props in buffer if ignoreAllUnknown is true (FasterXML#3082)
Dont track unknown props in buffer if ignoreAllUnknown is true. (cherry picked from commit 23f9c61)
1 parent e0fd026 commit 62a832e

File tree

3 files changed

+131
-5
lines changed

3 files changed

+131
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -506,12 +506,15 @@ protected Object _deserializeUsingPropertyBased(final JsonParser p, final Deseri
506506
}
507507
continue;
508508
}
509-
// Ok then, let's collect the whole field; name and value
510-
if (unknown == null) {
511-
unknown = new TokenBuffer(p, ctxt);
509+
510+
if(!_ignoreAllUnknown) {
511+
// Ok then, let's collect the whole field; name and value
512+
if (unknown == null) {
513+
unknown = new TokenBuffer(p, ctxt);
514+
}
515+
unknown.writeFieldName(propName);
516+
unknown.copyCurrentStructure(p);
512517
}
513-
unknown.writeFieldName(propName);
514-
unknown.copyCurrentStructure(p);
515518
}
516519

517520
// We hit END_OBJECT, so:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.fasterxml.jackson.databind.deser.filter;
2+
3+
import com.fasterxml.jackson.annotation.JsonAnyGetter;
4+
import com.fasterxml.jackson.annotation.JsonAnySetter;
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7+
import com.fasterxml.jackson.annotation.JsonProperty;
8+
import com.fasterxml.jackson.annotation.JsonUnwrapped;
9+
import com.fasterxml.jackson.databind.BaseMapTest;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
public class IgnoreUnknownPropertyUsingPropertyBasedTest extends BaseMapTest {
16+
17+
private final ObjectMapper MAPPER = newJsonMapper();
18+
19+
@JsonIgnoreProperties(ignoreUnknown = true)
20+
static class IgnoreUnknownAnySetter {
21+
22+
int a, b;
23+
24+
@JsonCreator
25+
public IgnoreUnknownAnySetter(@JsonProperty("a") int a, @JsonProperty("b") int b) {
26+
this.a = a;
27+
this.b = b;
28+
}
29+
30+
Map<String, Object> props = new HashMap<>();
31+
32+
@JsonAnySetter
33+
public void addProperty(String key, Object value) {
34+
props.put(key, value);
35+
}
36+
37+
@JsonAnyGetter
38+
public Map<String, Object> getProperties() {
39+
return props;
40+
}
41+
}
42+
43+
@JsonIgnoreProperties(ignoreUnknown = true)
44+
static class IgnoreUnknownUnwrapped {
45+
46+
int a, b;
47+
48+
@JsonCreator
49+
public IgnoreUnknownUnwrapped(@JsonProperty("a") int a, @JsonProperty("b") int b) {
50+
this.a = a;
51+
this.b = b;
52+
}
53+
54+
@JsonUnwrapped
55+
UnwrappedChild child;
56+
57+
static class UnwrappedChild {
58+
public int x, y;
59+
}
60+
}
61+
62+
public void testAnySetterWithFailOnUnknownDisabled() throws Exception {
63+
IgnoreUnknownAnySetter value = MAPPER.readValue("{\"a\":1, \"b\":2, \"x\":3, \"y\": 4}", IgnoreUnknownAnySetter.class);
64+
assertNotNull(value);
65+
assertEquals(1, value.a);
66+
assertEquals(2, value.b);
67+
assertEquals(3, value.props.get("x"));
68+
assertEquals(4, value.props.get("y"));
69+
assertEquals(2, value.props.size());
70+
}
71+
72+
public void testUnwrappedWithFailOnUnknownDisabled() throws Exception {
73+
IgnoreUnknownUnwrapped value = MAPPER.readValue("{\"a\":1, \"b\": 2, \"x\":3, \"y\":4}", IgnoreUnknownUnwrapped.class);
74+
assertNotNull(value);
75+
assertEquals(1, value.a);
76+
assertEquals(2, value.b);
77+
assertEquals(3, value.child.x);
78+
assertEquals(4, value.child.y);
79+
}
80+
81+
}

src/test/java/com/fasterxml/jackson/databind/deser/filter/TestUnknownPropertyDeserialization.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ static class IgnoreUnknown {
7878
public int a;
7979
}
8080

81+
@JsonIgnoreProperties(ignoreUnknown=true)
82+
static class IgnoreUnknownAnySetter {
83+
84+
Map<String, Object> props = new HashMap<>();
85+
86+
@JsonAnySetter
87+
public void addProperty(String key, Object value) {
88+
props.put(key, value);
89+
}
90+
91+
@JsonAnyGetter
92+
public Map<String, Object> getProperties() {
93+
return props;
94+
}
95+
}
96+
97+
@JsonIgnoreProperties(ignoreUnknown=true)
98+
static class IgnoreUnknownUnwrapped {
99+
100+
@JsonUnwrapped
101+
UnwrappedChild child;
102+
103+
static class UnwrappedChild {
104+
public int a, b;
105+
}
106+
}
107+
81108
@SuppressWarnings("serial")
82109
@JsonIgnoreProperties({"a", "d"})
83110
static class IgnoreMap extends HashMap<String,Object> { }
@@ -228,6 +255,21 @@ public void testClassWithIgnoreUnknown() throws Exception
228255
assertEquals(-3, result.a);
229256
}
230257

258+
public void testAnySetterWithFailOnUnknownDisabled() throws Exception
259+
{
260+
IgnoreUnknownAnySetter value = MAPPER.readValue("{\"x\":\"y\", \"a\":\"b\"}", IgnoreUnknownAnySetter.class);
261+
assertNotNull(value);
262+
assertEquals(2, value.props.size());
263+
}
264+
265+
public void testUnwrappedWithFailOnUnknownDisabled() throws Exception
266+
{
267+
IgnoreUnknownUnwrapped value = MAPPER.readValue("{\"a\":1, \"b\":2}", IgnoreUnknownUnwrapped.class);
268+
assertNotNull(value);
269+
assertEquals(1, value.child.a);
270+
assertEquals(2, value.child.b);
271+
}
272+
231273
/**
232274
* Test that verifies that use of {@link JsonIgnore} will add implicit
233275
* skipping of matching properties.

0 commit comments

Comments
 (0)