|
| 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 | +} |
0 commit comments