Skip to content

Commit 9965bfa

Browse files
committed
add test cases for ingore unknown fields + @JsonAnySetter and @JsonUnwrapped
1 parent 7153dd1 commit 9965bfa

File tree

2 files changed

+123
-0
lines changed

2 files changed

+123
-0
lines changed
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
@@ -71,6 +71,33 @@ static class IgnoreUnknown {
7171
public int a;
7272
}
7373

74+
@JsonIgnoreProperties(ignoreUnknown=true)
75+
static class IgnoreUnknownAnySetter {
76+
77+
Map<String, Object> props = new HashMap<>();
78+
79+
@JsonAnySetter
80+
public void addProperty(String key, Object value) {
81+
props.put(key, value);
82+
}
83+
84+
@JsonAnyGetter
85+
public Map<String, Object> getProperties() {
86+
return props;
87+
}
88+
}
89+
90+
@JsonIgnoreProperties(ignoreUnknown=true)
91+
static class IgnoreUnknownUnwrapped {
92+
93+
@JsonUnwrapped
94+
UnwrappedChild child;
95+
96+
static class UnwrappedChild {
97+
public int a, b;
98+
}
99+
}
100+
74101
@SuppressWarnings("serial")
75102
@JsonIgnoreProperties({"a", "d"})
76103
static class IgnoreMap extends HashMap<String,Object> { }
@@ -224,6 +251,21 @@ public void testClassWithIgnoreUnknown() throws Exception
224251
assertEquals(-3, result.a);
225252
}
226253

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

0 commit comments

Comments
 (0)