Skip to content

Commit 6cf6585

Browse files
committed
Add a test, work-round for #543
1 parent 4294ffc commit 6cf6585

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ Version: 2.5.0 (xx-xxx-2014)
44
#521: Keep bundle annotations, prevent problems with recursive annotation
55
types
66
(reported by tea-dragon@github)
7+
#543: Problem resolving self-referential recursive types
8+
(reported by ahgittin@github)
79

810
------------------------------------------------------------------------
911
=== History: ===

src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ protected void _resolveBindings(Type t)
299299
}
300300
_addPlaceholder(name); // to prevent infinite loops
301301

302-
if (typeParams != null) {
302+
if (typeParams != null && typeParams.length > i) {
303303
_bindings.put(name, typeParams[i]);
304304
} else {
305305
_bindings.put(name, _typeFactory._constructType(varType, this));

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

+28-6
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.fasterxml.jackson.databind.*;
1212
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
1313
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
14-
import com.fasterxml.jackson.databind.type.TypeFactory;
1514

1615
public class TestWithGenerics extends BaseMapTest
1716
{
@@ -45,7 +44,11 @@ static class ContainerWithField<T extends Animal> {
4544
public ContainerWithField(T a) { animal = a; }
4645
}
4746

48-
// Beans for [JACKSON-387], [JACKSON-430]
47+
static class WrappedContainerWithField {
48+
public ContainerWithField<?> animalContainer;
49+
}
50+
51+
// Beans for [JACKSON-387], [JACKSON-430]
4952

5053
@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@classAttr1")
5154
static class MyClass {
@@ -114,17 +117,28 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov
114117
}
115118
}
116119

120+
// [Issue#543]
121+
static class ContainerWithTwoAnimals<U extends Animal,V extends Animal> extends ContainerWithField<U> {
122+
public V otherAnimal;
123+
124+
public ContainerWithTwoAnimals(U a1, V a2) {
125+
super(a1);
126+
otherAnimal = a2;
127+
}
128+
}
117129

118130
/*
119131
/**********************************************************
120132
/* Unit tests
121133
/**********************************************************
122134
*/
123135

136+
private final ObjectMapper MAPPER = objectMapper();
137+
124138
public void testWrapperWithGetter() throws Exception
125139
{
126140
Dog dog = new Dog("Fluffy", 3);
127-
String json = new ObjectMapper().writeValueAsString(new ContainerWithGetter<Animal>(dog));
141+
String json = MAPPER.writeValueAsString(new ContainerWithGetter<Animal>(dog));
128142
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
129143
fail("polymorphic type not kept, result == "+json+"; should contain 'object-type':'...'");
130144
}
@@ -133,7 +147,7 @@ public void testWrapperWithGetter() throws Exception
133147
public void testWrapperWithField() throws Exception
134148
{
135149
Dog dog = new Dog("Fluffy", 3);
136-
String json = new ObjectMapper().writeValueAsString(new ContainerWithField<Animal>(dog));
150+
String json = MAPPER.writeValueAsString(new ContainerWithField<Animal>(dog));
137151
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
138152
fail("polymorphic type not kept, result == "+json+"; should contain 'object-type':'...'");
139153
}
@@ -143,8 +157,7 @@ public void testWrapperWithExplicitType() throws Exception
143157
{
144158
Dog dog = new Dog("Fluffy", 3);
145159
ContainerWithGetter<Animal> c2 = new ContainerWithGetter<Animal>(dog);
146-
ObjectMapper mapper = new ObjectMapper();
147-
String json = mapper.writerWithType(TypeFactory.defaultInstance().constructParametricType(ContainerWithGetter.class, Animal.class)).writeValueAsString(c2);
160+
String json = MAPPER.writerWithType(MAPPER.getTypeFactory().constructParametricType(ContainerWithGetter.class, Animal.class)).writeValueAsString(c2);
148161
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
149162
fail("polymorphic type not kept, result == "+json+"; should contain 'object-type':'...'");
150163
}
@@ -201,4 +214,13 @@ public void testJackson430() throws Exception
201214
assertNotNull(mc2.params);
202215
assertEquals(1, mc2.params.size());
203216
}
217+
218+
// [Issue#543]
219+
public void testValueWithMoreGenericParameters() throws Exception
220+
{
221+
WrappedContainerWithField wrappedContainerWithField = new WrappedContainerWithField();
222+
wrappedContainerWithField.animalContainer = new ContainerWithTwoAnimals<Dog,Dog>(new Dog("d1",1), new Dog("d2",2));
223+
String json = MAPPER.writeValueAsString(wrappedContainerWithField);
224+
assertNotNull(json);
225+
}
204226
}

0 commit comments

Comments
 (0)