Skip to content

Commit

Permalink
Add a test, work-round for #543
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 15, 2014
1 parent 4294ffc commit 6cf6585
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Version: 2.5.0 (xx-xxx-2014)
#521: Keep bundle annotations, prevent problems with recursive annotation
types
(reported by tea-dragon@github)
#543: Problem resolving self-referential recursive types
(reported by ahgittin@github)

------------------------------------------------------------------------
=== History: ===
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ protected void _resolveBindings(Type t)
}
_addPlaceholder(name); // to prevent infinite loops

if (typeParams != null) {
if (typeParams != null && typeParams.length > i) {
_bindings.put(name, typeParams[i]);
} else {
_bindings.put(name, _typeFactory._constructType(varType, this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ser.BeanSerializerFactory;
import com.fasterxml.jackson.databind.ser.ResolvableSerializer;
import com.fasterxml.jackson.databind.type.TypeFactory;

public class TestWithGenerics extends BaseMapTest
{
Expand Down Expand Up @@ -45,7 +44,11 @@ static class ContainerWithField<T extends Animal> {
public ContainerWithField(T a) { animal = a; }
}

// Beans for [JACKSON-387], [JACKSON-430]
static class WrappedContainerWithField {
public ContainerWithField<?> animalContainer;
}

// Beans for [JACKSON-387], [JACKSON-430]

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

// [Issue#543]
static class ContainerWithTwoAnimals<U extends Animal,V extends Animal> extends ContainerWithField<U> {
public V otherAnimal;

public ContainerWithTwoAnimals(U a1, V a2) {
super(a1);
otherAnimal = a2;
}
}

/*
/**********************************************************
/* Unit tests
/**********************************************************
*/

private final ObjectMapper MAPPER = objectMapper();

public void testWrapperWithGetter() throws Exception
{
Dog dog = new Dog("Fluffy", 3);
String json = new ObjectMapper().writeValueAsString(new ContainerWithGetter<Animal>(dog));
String json = MAPPER.writeValueAsString(new ContainerWithGetter<Animal>(dog));
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
fail("polymorphic type not kept, result == "+json+"; should contain 'object-type':'...'");
}
Expand All @@ -133,7 +147,7 @@ public void testWrapperWithGetter() throws Exception
public void testWrapperWithField() throws Exception
{
Dog dog = new Dog("Fluffy", 3);
String json = new ObjectMapper().writeValueAsString(new ContainerWithField<Animal>(dog));
String json = MAPPER.writeValueAsString(new ContainerWithField<Animal>(dog));
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
fail("polymorphic type not kept, result == "+json+"; should contain 'object-type':'...'");
}
Expand All @@ -143,8 +157,7 @@ public void testWrapperWithExplicitType() throws Exception
{
Dog dog = new Dog("Fluffy", 3);
ContainerWithGetter<Animal> c2 = new ContainerWithGetter<Animal>(dog);
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writerWithType(TypeFactory.defaultInstance().constructParametricType(ContainerWithGetter.class, Animal.class)).writeValueAsString(c2);
String json = MAPPER.writerWithType(MAPPER.getTypeFactory().constructParametricType(ContainerWithGetter.class, Animal.class)).writeValueAsString(c2);
if (json.indexOf("\"object-type\":\"doggy\"") < 0) {
fail("polymorphic type not kept, result == "+json+"; should contain 'object-type':'...'");
}
Expand Down Expand Up @@ -201,4 +214,13 @@ public void testJackson430() throws Exception
assertNotNull(mc2.params);
assertEquals(1, mc2.params.size());
}

// [Issue#543]
public void testValueWithMoreGenericParameters() throws Exception
{
WrappedContainerWithField wrappedContainerWithField = new WrappedContainerWithField();
wrappedContainerWithField.animalContainer = new ContainerWithTwoAnimals<Dog,Dog>(new Dog("d1",1), new Dog("d2",2));
String json = MAPPER.writeValueAsString(wrappedContainerWithField);
assertNotNull(json);
}
}

0 comments on commit 6cf6585

Please sign in to comment.