Skip to content

Commit 915dee7

Browse files
committed
Merge manually test for #2515
1 parent ab9c920 commit 915dee7

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/test/java/com/fasterxml/jackson/databind/jsontype/TestSubtypes.java

+61
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.*;
44

5+
import com.fasterxml.jackson.annotation.JsonProperty;
56
import com.fasterxml.jackson.annotation.JsonSubTypes;
67
import com.fasterxml.jackson.annotation.JsonTypeInfo;
78
import com.fasterxml.jackson.annotation.JsonTypeName;
@@ -143,6 +144,32 @@ static class Factory1311ImplA implements Factory1311 { }
143144
@JsonTypeName("implB")
144145
static class Factory1311ImplB implements Factory1311 { }
145146

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+
146173
/*
147174
/**********************************************************************
148175
/* Unit tests
@@ -319,4 +346,38 @@ public void testIssue1125WithDefault() throws Exception
319346
assertEquals(5, impl.b);
320347
assertEquals(9, impl.def);
321348
}
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+
}
322383
}

0 commit comments

Comments
 (0)