|
5 | 5 |
|
6 | 6 | import com.fasterxml.jackson.databind.BaseMapTest;
|
7 | 7 | import com.fasterxml.jackson.databind.ObjectMapper;
|
| 8 | +import com.fasterxml.jackson.databind.exc.InvalidDefinitionException; |
8 | 9 | import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
|
9 | 10 |
|
10 | 11 | import java.util.List;
|
@@ -72,6 +73,28 @@ static class BaseForNameAndNamesTest {
|
72 | 73 | public MultiTypeName getData() { return data; }
|
73 | 74 | }
|
74 | 75 |
|
| 76 | + static class WrapperForNotUniqueNamesTest { |
| 77 | + List<BaseForNotUniqueNamesTest> base; |
| 78 | + public List<BaseForNotUniqueNamesTest> getBase() { return base; } |
| 79 | + } |
| 80 | + |
| 81 | + static class BaseForNotUniqueNamesTest { |
| 82 | + private String type; |
| 83 | + public String getType() { return type; } |
| 84 | + |
| 85 | + @JsonTypeInfo( |
| 86 | + use = JsonTypeInfo.Id.NAME, |
| 87 | + include = JsonTypeInfo.As.EXTERNAL_PROPERTY, |
| 88 | + property = "type" |
| 89 | + ) |
| 90 | + @JsonSubTypes(value = { |
| 91 | + @JsonSubTypes.Type(value = A.class, name = "a"), |
| 92 | + @JsonSubTypes.Type(value = B.class, names = {"b","a"}), |
| 93 | + }, allowRepeatedNames = false) |
| 94 | + MultiTypeName data; |
| 95 | + public MultiTypeName getData() { return data; } |
| 96 | + } |
| 97 | + |
75 | 98 | /*
|
76 | 99 | /**********************************************************
|
77 | 100 | /* Test methods
|
@@ -133,4 +156,17 @@ public void testNameAndNames() throws Exception
|
133 | 156 | verifyException(e, "Unrecognized field \"data\"");
|
134 | 157 | }
|
135 | 158 | }
|
| 159 | + |
| 160 | + public void testNotUniqueNameAndNames() throws Exception |
| 161 | + { |
| 162 | + String json = "{\"base\": [{\"type\":\"a\", \"data\": {\"x\": 5}}, {\"type\":\"b\", \"data\": {\"y\": 3.1}}, {\"type\":\"c\", \"data\": {\"y\": 33.8}}]}"; |
| 163 | + |
| 164 | + try { |
| 165 | + MAPPER.readValue(json, WrapperForNotUniqueNamesTest.class); |
| 166 | + fail("This serialisation should fail because of repeated subtype name"); |
| 167 | + } catch (InvalidDefinitionException e) { |
| 168 | + verifyException(e, "Annotated type [data] got repeated subtype name [a]"); |
| 169 | + } |
| 170 | + } |
| 171 | + |
136 | 172 | }
|
0 commit comments