|
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.module.SimpleModule;
|
@@ -38,6 +41,43 @@ public Map<String,String> getAny(){
|
38 | 41 | }
|
39 | 42 | }
|
40 | 43 |
|
| 44 | + // [databind#1601] |
| 45 | + static class RootData { |
| 46 | + public String name; |
| 47 | + public String type; |
| 48 | + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, |
| 49 | + property = "type") |
| 50 | + @JsonSubTypes({ |
| 51 | + @Type(value = TypeA.class, name = "TypeA"), |
| 52 | + @Type(value = TypeB.class, name = "TypeB")}) |
| 53 | + public Proxy proxy; |
| 54 | + |
| 55 | + public RootData() {} |
| 56 | + |
| 57 | + public RootData(String name, String type, Proxy proxy) { |
| 58 | + this.name = name; |
| 59 | + this.type = type; |
| 60 | + this.proxy = proxy; |
| 61 | + } |
| 62 | + } |
| 63 | + static interface Proxy { } |
| 64 | + |
| 65 | + static class TypeA implements Proxy { |
| 66 | + public String aValue; |
| 67 | + public TypeA() {} |
| 68 | + public TypeA(String a) { |
| 69 | + this.aValue = a; |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + static class TypeB implements Proxy { |
| 74 | + public String bValue; |
| 75 | + public TypeB() {} |
| 76 | + public TypeB(String b) { |
| 77 | + this.bValue = b; |
| 78 | + } |
| 79 | + } |
| 80 | + |
41 | 81 | /*
|
42 | 82 | /**********************************************************
|
43 | 83 | /* Test methods
|
@@ -136,4 +176,21 @@ public void testMapOfNulls() throws Exception
|
136 | 176 | assertEquals(1, deser.size());
|
137 | 177 | assertEquals("funny", deser.get("key"));
|
138 | 178 | }
|
| 179 | + |
| 180 | + // [databind#1601] |
| 181 | + public void testPolymorphicDataNull() throws Exception { |
| 182 | + ObjectMapper mapper = new ObjectMapper(); |
| 183 | + String typeA = |
| 184 | + "{\"name\":\"TypeAData\", \"type\":\"TypeA\", \"proxy\":{\"aValue\":\"This works!\"}}"; |
| 185 | + RootData typeAData = mapper.readValue(typeA, RootData.class); |
| 186 | + assertEquals("No value for aValue!?", "This works!", ((TypeA) typeAData.proxy).aValue); |
| 187 | + String typeB = |
| 188 | + "{\"name\":\"TypeBData\", \"type\":\"TypeB\", \"proxy\":{\"bValue\":\"This works too!\"}}"; |
| 189 | + RootData typeBData = mapper.readValue(typeB, RootData.class); |
| 190 | + assertEquals("No value for bValue!?", "This works too!", ((TypeB) typeBData.proxy).bValue); |
| 191 | + String typeBNull = |
| 192 | + "{\"name\":\"TypeBData\", \"type\":\"TypeB\", \"proxy\": null}"; |
| 193 | + RootData typeBNullData = mapper.readValue(typeBNull, RootData.class); |
| 194 | + assertNull("Proxy should be null!", typeBNullData.proxy); |
| 195 | + } |
139 | 196 | }
|
0 commit comments