|
7 | 7 |
|
8 | 8 | import com.fasterxml.jackson.databind.BaseMapTest;
|
9 | 9 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 10 | +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; |
| 11 | +import com.fasterxml.jackson.databind.json.JsonMapper; |
10 | 12 |
|
11 | 13 | public class IgnorePropertyOnDeserTest extends BaseMapTest
|
12 | 14 | {
|
@@ -121,36 +123,60 @@ public void testIgnoreOnProperty1217() throws Exception
|
121 | 123 | assertEquals(2, result1.obj2.y);
|
122 | 124 | }
|
123 | 125 |
|
| 126 | + // [databind#1217] |
124 | 127 | public void testIgnoreViaConfigOverride1217() throws Exception
|
125 | 128 | {
|
126 |
| - ObjectMapper mapper = new ObjectMapper(); |
127 |
| - mapper.configOverride(Point.class) |
128 |
| - .setIgnorals(JsonIgnoreProperties.Value.forIgnoredProperties("y")); |
| 129 | + ObjectMapper mapper = JsonMapper.builder() |
| 130 | + .withConfigOverride(Point.class, |
| 131 | + cfg -> cfg.setIgnorals(JsonIgnoreProperties.Value.forIgnoredProperties("y"))) |
| 132 | + .build(); |
129 | 133 | Point p = mapper.readValue(a2q("{'x':1,'y':2}"), Point.class);
|
130 | 134 | // bind 'x', but ignore 'y'
|
131 | 135 | assertEquals(1, p.x);
|
132 | 136 | assertEquals(0, p.y);
|
133 | 137 | }
|
134 | 138 |
|
| 139 | + // [databind#3721] |
| 140 | + public void testIgnoreUnknownViaConfigOverride() throws Exception |
| 141 | + { |
| 142 | + final String DOC = a2q("{'x':2,'foobar':3}"); |
| 143 | + |
| 144 | + // First, fail without overrides |
| 145 | + try { |
| 146 | + MAPPER.readValue(DOC, Point.class); |
| 147 | + fail("Should not pass"); |
| 148 | + } catch (UnrecognizedPropertyException e) { |
| 149 | + verifyException(e, "foobar"); // message varies between 2.x and 3.x |
| 150 | + } |
| 151 | + |
| 152 | + // But pass with specific class override: |
| 153 | + ObjectMapper mapper = JsonMapper.builder() |
| 154 | + .withConfigOverride(Point.class, |
| 155 | + cfg -> cfg.setIgnorals(JsonIgnoreProperties.Value.forIgnoreUnknown(true))) |
| 156 | + .build(); |
| 157 | + Point p = mapper.readValue(DOC, Point.class); |
| 158 | + assertEquals(2, p.x); |
| 159 | + |
| 160 | + // 13-Jan-2023, tatu: Alas, no global defaulting yet! |
| 161 | + } |
| 162 | + |
135 | 163 | // [databind#1595]
|
136 | 164 | public void testIgnoreGetterNotSetter1595() throws Exception
|
137 | 165 | {
|
138 |
| - ObjectMapper mapper = new ObjectMapper(); |
139 | 166 | Simple1595 config = new Simple1595();
|
140 | 167 | config.setId(123);
|
141 | 168 | config.setName("jack");
|
142 |
| - String json = mapper.writeValueAsString(config); |
| 169 | + String json = MAPPER.writeValueAsString(config); |
143 | 170 | assertEquals(a2q("{'id':123}"), json);
|
144 |
| - Simple1595 des = mapper.readValue(a2q("{'id':123,'name':'jack'}"), Simple1595.class); |
| 171 | + Simple1595 des = MAPPER.readValue(a2q("{'id':123,'name':'jack'}"), Simple1595.class); |
145 | 172 | assertEquals("jack", des.getName());
|
146 | 173 | }
|
147 | 174 |
|
148 | 175 | // [databind#2627]
|
149 | 176 | public void testIgnoreUnknownOnField() throws IOException
|
150 | 177 | {
|
151 |
| - ObjectMapper objectMapper = new ObjectMapper(); |
152 | 178 | String json = "{\"value\": {\"name\": \"my_name\", \"extra\": \"val\"}, \"type\":\"Json\"}";
|
153 |
| - MyPojoValue value = objectMapper.readValue(json, MyPojoValue.class); |
| 179 | + MyPojoValue value = MAPPER.readValue(json, MyPojoValue.class); |
154 | 180 | assertNotNull(value);
|
155 | 181 | assertNotNull(value.getValue());
|
156 | 182 | assertEquals("my_name", value.getValue().name);
|
|
0 commit comments