Skip to content

Commit 0238f95

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

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

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

Lines changed: 44 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,23 @@ public void testClassWithIgnoreUnknown() throws Exception
224251
assertEquals(-3, result.a);
225252
}
226253

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

0 commit comments

Comments
 (0)