|
6 | 6 | import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
|
7 | 7 | import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
|
8 | 8 | import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
|
9 |
| -import com.fasterxml.jackson.annotation.JsonPropertyOrder; |
10 | 9 |
|
11 | 10 | import com.fasterxml.jackson.core.type.TypeReference;
|
12 | 11 |
|
@@ -192,10 +191,26 @@ static class Bean1635A extends Bean1635 {
|
192 | 191 |
|
193 | 192 | static class Bean1635Default extends Bean1635 { }
|
194 | 193 |
|
| 194 | + // [databind#3271] |
| 195 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, |
| 196 | + visible = true, property = "type", defaultImpl = DefaultShape3271.class) |
| 197 | + @JsonSubTypes({@JsonSubTypes.Type(value = Square3271.class, name = "square")}) |
| 198 | + static abstract class Shape3271 { |
| 199 | + public String type; |
| 200 | + |
| 201 | + public String getType() { return this.type; } |
| 202 | + |
| 203 | + public void setType(String type) { this.type = type; } |
| 204 | + } |
| 205 | + |
| 206 | + static class Square3271 extends Shape3271 {} |
| 207 | + |
| 208 | + static class DefaultShape3271 extends Shape3271 {} |
| 209 | + |
195 | 210 | /*
|
196 |
| - /********************************************************** |
| 211 | + /********************************************************************** |
197 | 212 | /* Mock data
|
198 |
| - /********************************************************** |
| 213 | + /********************************************************************** |
199 | 214 | */
|
200 | 215 |
|
201 | 216 | private static final Orange mandarin = new Orange("Mandarin Orange", "orange");
|
@@ -226,12 +241,12 @@ static class Bean1635Default extends Bean1635 { }
|
226 | 241 | private static final String carListJson = "[" + camryJson + "," + accordJson + "]";
|
227 | 242 |
|
228 | 243 | /*
|
229 |
| - /********************************************************** |
| 244 | + /********************************************************************** |
230 | 245 | /* Test methods
|
231 |
| - /********************************************************** |
| 246 | + /********************************************************************** |
232 | 247 | */
|
233 | 248 |
|
234 |
| - private final ObjectMapper MAPPER = new ObjectMapper(); |
| 249 | + private final ObjectMapper MAPPER = newJsonMapper(); |
235 | 250 |
|
236 | 251 | /**
|
237 | 252 | * Fruits - serialization tests for simple property on sub-classes
|
@@ -443,4 +458,21 @@ public void testExistingEnumTypeIdViaDefault() throws Exception
|
443 | 458 | assertEquals(Bean1635Default.class, result.getClass());
|
444 | 459 | assertEquals(ABC.C, result.type);
|
445 | 460 | }
|
| 461 | + |
| 462 | + // [databind#3271]: verify that `null` token does not become "null" String |
| 463 | + |
| 464 | + public void testDeserializationWithValidType() throws Exception { |
| 465 | + Shape3271 deserShape = MAPPER.readValue("{\"type\":\"square\"}", Shape3271.class); |
| 466 | + assertEquals("square", deserShape.getType()); |
| 467 | + } |
| 468 | + |
| 469 | + public void testDeserializationWithInvalidType() throws Exception { |
| 470 | + Shape3271 deserShape = MAPPER.readValue("{\"type\":\"invalid\"}", Shape3271.class); |
| 471 | + assertEquals("invalid", deserShape.getType()); |
| 472 | + } |
| 473 | + |
| 474 | + public void testDeserializationNull() throws Exception { |
| 475 | + Shape3271 deserShape = MAPPER.readValue("{\"type\":null}", Shape3271.class); |
| 476 | + assertNull(deserShape.getType()); // error: "expected null, but was:<null>" |
| 477 | + } |
446 | 478 | }
|
0 commit comments