|
7 | 7 | import java.util.Map;
|
8 | 8 |
|
9 | 9 | import com.fasterxml.jackson.annotation.JsonAnySetter;
|
| 10 | +import com.fasterxml.jackson.annotation.JsonSubTypes; |
| 11 | +import com.fasterxml.jackson.annotation.JsonSubTypes.Type; |
| 12 | +import com.fasterxml.jackson.annotation.JsonTypeInfo; |
10 | 13 | import com.fasterxml.jackson.core.*;
|
11 | 14 | import com.fasterxml.jackson.databind.*;
|
12 | 15 | import com.fasterxml.jackson.databind.deser.JDKScalarsTest.PrimitivesBean;
|
@@ -38,9 +41,46 @@ public Map<String,String> getAny(){
|
38 | 41 | return this.any;
|
39 | 42 | }
|
40 | 43 | }
|
| 44 | + |
| 45 | + // [databind#1601] |
| 46 | + static class RootData { |
| 47 | + public String name; |
| 48 | + public String type; |
| 49 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, |
| 50 | + property = "type") |
| 51 | + @JsonSubTypes({ |
| 52 | + @Type(value = TypeA.class, name = "TypeA"), |
| 53 | + @Type(value = TypeB.class, name = "TypeB")}) |
| 54 | + public Proxy proxy; |
| 55 | + |
| 56 | + public RootData() {} |
| 57 | + |
| 58 | + public RootData(String name, String type, Proxy proxy) { |
| 59 | + this.name = name; |
| 60 | + this.type = type; |
| 61 | + this.proxy = proxy; |
| 62 | + } |
| 63 | + } |
| 64 | + static interface Proxy { } |
| 65 | + |
| 66 | + static class TypeA implements Proxy { |
| 67 | + public String aValue; |
| 68 | + public TypeA() {} |
| 69 | + public TypeA(String a) { |
| 70 | + this.aValue = a; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + static class TypeB implements Proxy { |
| 75 | + public String bValue; |
| 76 | + public TypeB() {} |
| 77 | + public TypeB(String b) { |
| 78 | + this.bValue = b; |
| 79 | + } |
| 80 | + } |
41 | 81 |
|
42 | 82 | private final ObjectMapper MAPPER = objectMapper();
|
43 |
| - |
| 83 | + |
44 | 84 | /*
|
45 | 85 | /**********************************************************
|
46 | 86 | /* Test methods
|
@@ -221,4 +261,21 @@ public void testMapOfNulls() throws Exception
|
221 | 261 | assertEquals(1, deser.size());
|
222 | 262 | assertEquals("funny", deser.get("key"));
|
223 | 263 | }
|
| 264 | + |
| 265 | + // [databind#1601] |
| 266 | + public void testPolymorphicDataNull() throws Exception |
| 267 | + { |
| 268 | + String typeA = |
| 269 | + "{\"name\":\"TypeAData\", \"type\":\"TypeA\", \"proxy\":{\"aValue\":\"This works!\"}}"; |
| 270 | + RootData typeAData = MAPPER.readValue(typeA, RootData.class); |
| 271 | + assertEquals("No value for aValue!?", "This works!", ((TypeA) typeAData.proxy).aValue); |
| 272 | + String typeB = |
| 273 | + "{\"name\":\"TypeBData\", \"type\":\"TypeB\", \"proxy\":{\"bValue\":\"This works too!\"}}"; |
| 274 | + RootData typeBData = MAPPER.readValue(typeB, RootData.class); |
| 275 | + assertEquals("No value for bValue!?", "This works too!", ((TypeB) typeBData.proxy).bValue); |
| 276 | + String typeBNull = |
| 277 | + "{\"name\":\"TypeBData\", \"type\":\"TypeB\", \"proxy\": null}"; |
| 278 | + RootData typeBNullData = MAPPER.readValue(typeBNull, RootData.class); |
| 279 | + assertNull("Proxy should be null!", typeBNullData.proxy); |
| 280 | + } |
224 | 281 | }
|
0 commit comments