Skip to content

Commit

Permalink
Backport #543 workaround in 2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Sep 15, 2014
1 parent d34aab5 commit 9c2dc36
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
1 change: 1 addition & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Version: 2.4.3 (xx-xxx-2014)
(reported by Ian B, tea-dragon@github)
#524: @JsonIdentityReference(alwaysAsId = true) Custom resolver is reset to SimpleObjectIdResolver
(reported by pkokorev@github)
#543: Problem resolving self-referential generic types
- Fixed a problem with `acceptJsonFormatVisitor` with Collection/array types that
are marked with `@JsonValue`; could cause NPE in JSON Schema generator module.

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 9c2dc36

Please sign in to comment.