Tatu Saloranta | ea66599 | 2017-02-02 21:54:45 -0800 | [diff] [blame] | 1 | package com.fasterxml.jackson.databind.deser.filter; |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 2 | |
| 3 | import java.io.*; |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 4 | import java.util.*; |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 5 | |
| 6 | import com.fasterxml.jackson.annotation.*; |
| 7 | import com.fasterxml.jackson.core.*; |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 8 | import com.fasterxml.jackson.core.type.TypeReference; |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 9 | import com.fasterxml.jackson.databind.*; |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 10 | import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 11 | |
| 12 | /** |
| 13 | * Unit tests for checking handling of unknown properties |
| 14 | */ |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 15 | public class TestUnknownPropertyDeserialization |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 16 | extends BaseMapTest |
| 17 | { |
| 18 | final static String JSON_UNKNOWN_FIELD = "{ \"a\" : 1, \"foo\" : [ 1, 2, 3], \"b\" : -1 }"; |
| 19 | |
| 20 | /* |
| 21 | /********************************************************** |
| 22 | /* Helper classes |
| 23 | /********************************************************** |
| 24 | */ |
| 25 | |
| 26 | final static class TestBean |
| 27 | { |
| 28 | String _unknown; |
| 29 | |
| 30 | int _a, _b; |
| 31 | |
| 32 | public TestBean() { } |
| 33 | |
| 34 | public void setA(int a) { _a = a; } |
| 35 | public void setB(int b) { _b = b; } |
| 36 | |
| 37 | public void markUnknown(String unk) { _unknown = unk; } |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * Simple {@link DeserializationProblemHandler} sub-class that |
| 42 | * just marks unknown property/ies when encountered, along with |
| 43 | * Json value of the property. |
| 44 | */ |
Tatu Saloranta | f2c40cb | 2016-05-10 20:21:32 -0700 | [diff] [blame] | 45 | static class MyHandler |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 46 | extends DeserializationProblemHandler |
| 47 | { |
| 48 | @Override |
Tatu Saloranta | 060ce11 | 2012-02-01 22:18:09 -0800 | [diff] [blame] | 49 | public boolean handleUnknownProperty(DeserializationContext ctxt, |
| 50 | JsonParser jp, JsonDeserializer<?> deserializer, |
| 51 | Object bean, String propertyName) |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 52 | throws IOException, JsonProcessingException |
| 53 | { |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 54 | // very simple, just to verify that we do see correct token type |
| 55 | ((TestBean) bean).markUnknown(propertyName+":"+jp.getCurrentToken().toString()); |
| 56 | // Yup, we are good to go; must skip whatever value we'd have: |
| 57 | jp.skipChildren(); |
| 58 | return true; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | @JsonIgnoreProperties({"b", "c"}) |
| 63 | static class IgnoreSome |
| 64 | { |
| 65 | public int a, b; |
| 66 | private String c, d; |
| 67 | |
| 68 | public IgnoreSome() { } |
| 69 | |
| 70 | public String c() { return c; } |
| 71 | public void setC(String value) { c = value; } |
| 72 | public String d() { return d; } |
| 73 | public void setD(String value) { d = value; } |
| 74 | } |
| 75 | |
| 76 | @JsonIgnoreProperties(ignoreUnknown=true) |
| 77 | static class IgnoreUnknown { |
| 78 | public int a; |
| 79 | } |
| 80 | |
| 81 | @SuppressWarnings("serial") |
| 82 | @JsonIgnoreProperties({"a", "d"}) |
| 83 | static class IgnoreMap extends HashMap<String,Object> { } |
| 84 | |
| 85 | static class ImplicitIgnores { |
| 86 | @JsonIgnore public int a; |
| 87 | @JsonIgnore public void setB(int b) { } |
| 88 | public int c; |
| 89 | } |
| 90 | |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 91 | // // Ignored as per [JACKSON-787] |
| 92 | |
| 93 | static class XYZWrapper1 { |
| 94 | @JsonIgnoreProperties({"x"}) |
| 95 | public YZ value; |
| 96 | } |
| 97 | |
| 98 | static class YZ { |
| 99 | public int y, z; |
| 100 | } |
| 101 | |
| 102 | static class XYZWrapper2 { |
| 103 | @JsonIgnoreProperties({"y"}) |
| 104 | public X value; |
| 105 | } |
| 106 | |
| 107 | @JsonIgnoreProperties({"z"}) |
| 108 | static class X { |
| 109 | public int x; |
| 110 | } |
| 111 | |
| 112 | static class MapWithoutX { |
| 113 | @JsonIgnoreProperties("x") |
| 114 | public Map<String,Integer> values; |
| 115 | } |
Tatu | 1ae2d62 | 2014-05-15 14:13:09 -0700 | [diff] [blame] | 116 | |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 117 | // [databind#987] |
| 118 | static class Bean987 { |
| 119 | public String aProperty; |
| 120 | } |
| 121 | |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 122 | /* |
| 123 | /********************************************************** |
| 124 | /* Test methods |
| 125 | /********************************************************** |
| 126 | */ |
| 127 | |
Tatu Saloranta | b5efcf7 | 2017-04-19 10:49:59 -0700 | [diff] [blame^] | 128 | private final ObjectMapper MAPPER = newObjectMapper(); |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 129 | |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 130 | /** |
| 131 | * By default we should just get an exception if an unknown property |
| 132 | * is encountered |
| 133 | */ |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 134 | public void testUnknownHandlingDefault() throws Exception |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 135 | { |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 136 | try { |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 137 | MAPPER.readValue(new StringReader(JSON_UNKNOWN_FIELD), TestBean.class); |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 138 | } catch (JsonMappingException jex) { |
| 139 | verifyException(jex, "Unrecognized field \"foo\""); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Test that verifies that it is possible to ignore unknown properties using |
| 145 | * {@link DeserializationProblemHandler}. |
| 146 | */ |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 147 | public void testUnknownHandlingIgnoreWithHandler() throws Exception |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 148 | { |
Tatu Saloranta | b5efcf7 | 2017-04-19 10:49:59 -0700 | [diff] [blame^] | 149 | ObjectMapper mapper = newObjectMapper(); |
Tatu | dfed924 | 2012-01-19 12:31:44 -0800 | [diff] [blame] | 150 | mapper.clearProblemHandlers(); |
| 151 | mapper.addHandler(new MyHandler()); |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 152 | TestBean result = mapper.readValue(new StringReader(JSON_UNKNOWN_FIELD), TestBean.class); |
| 153 | assertNotNull(result); |
| 154 | assertEquals(1, result._a); |
| 155 | assertEquals(-1, result._b); |
| 156 | assertEquals("foo:START_ARRAY", result._unknown); |
| 157 | } |
| 158 | |
| 159 | /** |
shaiyallin | 07bc9b7 | 2012-07-01 17:32:00 +0300 | [diff] [blame] | 160 | * Test that verifies that it is possible to ignore unknown properties using |
| 161 | * {@link DeserializationProblemHandler} and an ObjectReader. |
| 162 | */ |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 163 | public void testUnknownHandlingIgnoreWithHandlerAndObjectReader() throws Exception |
shaiyallin | 07bc9b7 | 2012-07-01 17:32:00 +0300 | [diff] [blame] | 164 | { |
Tatu Saloranta | b5efcf7 | 2017-04-19 10:49:59 -0700 | [diff] [blame^] | 165 | ObjectMapper mapper = newObjectMapper(); |
shaiyallin | 07bc9b7 | 2012-07-01 17:32:00 +0300 | [diff] [blame] | 166 | mapper.clearProblemHandlers(); |
Cowtowncoder | a936f43 | 2015-05-12 12:33:46 -0700 | [diff] [blame] | 167 | TestBean result = mapper.readerFor(TestBean.class).withHandler(new MyHandler()).readValue(new StringReader(JSON_UNKNOWN_FIELD)); |
shaiyallin | 07bc9b7 | 2012-07-01 17:32:00 +0300 | [diff] [blame] | 168 | assertNotNull(result); |
| 169 | assertEquals(1, result._a); |
| 170 | assertEquals(-1, result._b); |
| 171 | assertEquals("foo:START_ARRAY", result._unknown); |
| 172 | } |
| 173 | |
| 174 | /** |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 175 | * Test for checking that it is also possible to simply suppress |
| 176 | * error reporting for unknown properties. |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 177 | */ |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 178 | public void testUnknownHandlingIgnoreWithFeature() throws Exception |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 179 | { |
Tatu Saloranta | b5efcf7 | 2017-04-19 10:49:59 -0700 | [diff] [blame^] | 180 | ObjectMapper mapper = newObjectMapper(); |
Tatu | 9610aff | 2012-02-02 11:30:08 -0800 | [diff] [blame] | 181 | mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 182 | TestBean result = null; |
| 183 | try { |
| 184 | result = mapper.readValue(new StringReader(JSON_UNKNOWN_FIELD), TestBean.class); |
| 185 | } catch (JsonMappingException jex) { |
| 186 | fail("Did not expect a problem, got: "+jex.getMessage()); |
| 187 | } |
| 188 | assertNotNull(result); |
| 189 | assertEquals(1, result._a); |
| 190 | assertNull(result._unknown); |
| 191 | assertEquals(-1, result._b); |
| 192 | } |
| 193 | |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 194 | public void testWithClassIgnore() throws Exception |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 195 | { |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 196 | IgnoreSome result = MAPPER.readValue("{ \"a\":1,\"b\":2,\"c\":\"x\",\"d\":\"y\"}", |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 197 | IgnoreSome.class); |
| 198 | // first: should deserialize 2 of properties normally |
| 199 | assertEquals(1, result.a); |
| 200 | assertEquals("y", result.d()); |
| 201 | // and not take other 2 |
| 202 | assertEquals(0, result.b); |
| 203 | assertNull(result.c()); |
| 204 | } |
| 205 | |
| 206 | /// @since 1.4 |
| 207 | public void testClassIgnoreWithMap() throws Exception |
| 208 | { |
| 209 | // Let's actually use incompatible types for "a" and "d"; should not matter when ignored |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 210 | IgnoreMap result = MAPPER.readValue |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 211 | ("{ \"a\":[ 1],\n" |
| 212 | +"\"b\":2,\n" |
| 213 | +"\"c\": \"x\",\n" |
| 214 | +"\"d\":false }", IgnoreMap.class); |
| 215 | assertEquals(2, result.size()); |
| 216 | Object ob = result.get("b"); |
| 217 | assertEquals(Integer.class, ob.getClass()); |
| 218 | assertEquals(Integer.valueOf(2), ob); |
| 219 | assertEquals("x", result.get("c")); |
| 220 | assertFalse(result.containsKey("a")); |
| 221 | assertFalse(result.containsKey("d")); |
| 222 | } |
| 223 | |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 224 | public void testClassWithIgnoreUnknown() throws Exception |
| 225 | { |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 226 | IgnoreUnknown result = MAPPER.readValue |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 227 | ("{\"b\":3,\"c\":[1,2],\"x\":{ },\"a\":-3}", IgnoreUnknown.class); |
| 228 | assertEquals(-3, result.a); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * Test that verifies that use of {@link JsonIgnore} will add implicit |
| 233 | * skipping of matching properties. |
| 234 | */ |
| 235 | public void testClassWithUnknownAndIgnore() throws Exception |
| 236 | { |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 237 | // should be ok: "a" and "b" ignored, "c" mapped: |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 238 | ImplicitIgnores result = MAPPER.readValue |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 239 | ("{\"a\":1,\"b\":2,\"c\":3 }", ImplicitIgnores.class); |
| 240 | assertEquals(3, result.c); |
| 241 | |
| 242 | // but "d" is not defined, so should still error |
| 243 | try { |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 244 | MAPPER.readValue("{\"a\":1,\"b\":2,\"c\":3,\"d\":4 }", ImplicitIgnores.class); |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 245 | } catch (JsonMappingException e) { |
| 246 | verifyException(e, "Unrecognized field \"d\""); |
| 247 | } |
| 248 | } |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 249 | |
| 250 | public void testPropertyIgnoral() throws Exception |
| 251 | { |
| 252 | XYZWrapper1 result = MAPPER.readValue("{\"value\":{\"y\":2,\"x\":1,\"z\":3}}", XYZWrapper1.class); |
| 253 | assertEquals(2, result.value.y); |
| 254 | assertEquals(3, result.value.z); |
| 255 | } |
| 256 | |
| 257 | public void testPropertyIgnoralWithClass() throws Exception |
| 258 | { |
Tatu Saloranta | 24407a4 | 2016-05-01 14:07:51 -0700 | [diff] [blame] | 259 | XYZWrapper2 result = MAPPER.readValue("{\"value\":{\"y\":2,\"x\":1,\"z\":3}}", |
| 260 | XYZWrapper2.class); |
Tatu Saloranta | 15212e8 | 2012-02-20 22:13:25 -0800 | [diff] [blame] | 261 | assertEquals(1, result.value.x); |
| 262 | } |
| 263 | |
| 264 | public void testPropertyIgnoralForMap() throws Exception |
| 265 | { |
| 266 | MapWithoutX result = MAPPER.readValue("{\"values\":{\"x\":1,\"y\":2}}", MapWithoutX.class); |
| 267 | assertNotNull(result.values); |
| 268 | assertEquals(1, result.values.size()); |
| 269 | assertEquals(Integer.valueOf(2), result.values.get("y")); |
| 270 | } |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 271 | |
| 272 | public void testIssue987() throws Exception |
| 273 | { |
Tatu Saloranta | b5efcf7 | 2017-04-19 10:49:59 -0700 | [diff] [blame^] | 274 | ObjectMapper jsonMapper = newObjectMapper(); |
Cowtowncoder | 3bd5de6 | 2015-10-28 13:36:23 -0700 | [diff] [blame] | 275 | jsonMapper.addHandler(new DeserializationProblemHandler() { |
| 276 | @Override |
| 277 | public boolean handleUnknownProperty(DeserializationContext ctxt, JsonParser p, JsonDeserializer<?> deserializer, Object beanOrClass, String propertyName) throws IOException, JsonProcessingException { |
| 278 | p.skipChildren(); |
| 279 | return true; |
| 280 | } |
| 281 | }); |
| 282 | |
| 283 | String input = "[{\"aProperty\":\"x\",\"unknown\":{\"unknown\":{}}}]"; |
| 284 | List<Bean987> deserializedList = jsonMapper.readValue(input, |
| 285 | new TypeReference<List<Bean987>>() { }); |
| 286 | assertEquals(1, deserializedList.size()); |
| 287 | } |
Tatu Saloranta | a63c203 | 2011-12-24 00:26:53 -0800 | [diff] [blame] | 288 | } |