|
2 | 2 |
|
3 | 3 | import java.util.*;
|
4 | 4 |
|
| 5 | +import com.fasterxml.jackson.annotation.JsonProperty; |
5 | 6 | import com.fasterxml.jackson.annotation.JsonSubTypes;
|
6 | 7 | import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
7 | 8 | import com.fasterxml.jackson.annotation.JsonTypeName;
|
@@ -143,6 +144,32 @@ static class Factory1311ImplA implements Factory1311 { }
|
143 | 144 | @JsonTypeName("implB")
|
144 | 145 | static class Factory1311ImplB implements Factory1311 { }
|
145 | 146 |
|
| 147 | + // [databind#2515] |
| 148 | + @JsonTypeInfo(use=JsonTypeInfo.Id.NAME, include=As.PROPERTY, property="#type") |
| 149 | + static abstract class SuperTypeWithoutDefault { } |
| 150 | + |
| 151 | + static class Sub extends SuperTypeWithoutDefault { |
| 152 | + public int a; |
| 153 | + |
| 154 | + public Sub(){} |
| 155 | + public Sub(int a) { |
| 156 | + this.a = a; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + static class POJOWrapper { |
| 161 | + @JsonProperty |
| 162 | + Sub sub1; |
| 163 | + @JsonProperty |
| 164 | + Sub sub2; |
| 165 | + |
| 166 | + public POJOWrapper(){} |
| 167 | + public POJOWrapper(Sub sub1, Sub sub2) { |
| 168 | + this.sub1 = sub1; |
| 169 | + this.sub2 = sub2; |
| 170 | + } |
| 171 | + } |
| 172 | + |
146 | 173 | /*
|
147 | 174 | /**********************************************************************
|
148 | 175 | /* Unit tests
|
@@ -319,4 +346,38 @@ public void testIssue1125WithDefault() throws Exception
|
319 | 346 | assertEquals(5, impl.b);
|
320 | 347 | assertEquals(9, impl.def);
|
321 | 348 | }
|
| 349 | + |
| 350 | + // [databind#2525] |
| 351 | + public void testSerializationWithDuplicateRegisteredSubtypes() throws Exception { |
| 352 | + ObjectMapper mapper = jsonMapperBuilder() |
| 353 | + .registerSubtypes(new NamedType(Sub.class, "sub1")) |
| 354 | + .registerSubtypes(new NamedType(Sub.class, "sub2")) |
| 355 | + .build(); |
| 356 | + |
| 357 | + // the first registered type name is used for serialization |
| 358 | + Sub sub = new Sub(15); |
| 359 | + assertEquals("{\"#type\":\"sub1\",\"a\":15}", mapper.writeValueAsString(sub)); |
| 360 | + } |
| 361 | + |
| 362 | + // [databind#2525] |
| 363 | + public void testDeserializationWithDuplicateRegisteredSubtypes() throws Exception { |
| 364 | + ObjectMapper mapper = jsonMapperBuilder() |
| 365 | + // We can register the same class with different names |
| 366 | + .registerSubtypes(new NamedType(Sub.class, "sub1")) |
| 367 | + .registerSubtypes(new NamedType(Sub.class, "sub2")) |
| 368 | + .build(); |
| 369 | + |
| 370 | + // fields of a POJO will be deserialized correctly according to their field name |
| 371 | + POJOWrapper pojoWrapper = mapper.readValue("{\"sub1\":{\"#type\":\"sub1\",\"a\":10},\"sub2\":{\"#type\":\"sub2\",\"a\":50}}", POJOWrapper.class); |
| 372 | + assertEquals(10, pojoWrapper.sub1.a); |
| 373 | + assertEquals(50, pojoWrapper.sub2.a); |
| 374 | + |
| 375 | + // Instances of the same object can be deserialized with multiple names |
| 376 | + SuperTypeWithoutDefault sub1 = mapper.readValue("{\"#type\":\"sub1\", \"a\":20}", SuperTypeWithoutDefault.class); |
| 377 | + assertSame(Sub.class, sub1.getClass()); |
| 378 | + assertEquals(20, ((Sub) sub1).a); |
| 379 | + SuperTypeWithoutDefault sub2 = mapper.readValue("{\"#type\":\"sub2\", \"a\":30}", SuperTypeWithoutDefault.class); |
| 380 | + assertSame(Sub.class, sub2.getClass()); |
| 381 | + assertEquals(30, ((Sub) sub2).a); |
| 382 | + } |
322 | 383 | }
|
0 commit comments