From c9d89b415273ed807d83ac55ccdd80ee2eacbbbf Mon Sep 17 00:00:00 2001 From: Alex Heneveld Date: Sat, 13 Sep 2014 01:01:22 +0100 Subject: [PATCH 1/2] test case to illustrate jackson-543, generic widening serialization bug --- .../databind/jsontype/TestWithGenerics.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java index 08214a0c94..cd609be42c 100644 --- a/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java +++ b/src/test/java/com/fasterxml/jackson/databind/jsontype/TestWithGenerics.java @@ -45,6 +45,19 @@ static class ContainerWithField { public ContainerWithField(T a) { animal = a; } } + static class ContainerWithTwoAnimals extends ContainerWithField { + public V otherAnimal; + + public ContainerWithTwoAnimals(U a1, V a2) { + super(a1); + otherAnimal = a2; + } + } + + static class WrappedContainerWithField { + public ContainerWithField animalContainer; + } + // Beans for [JACKSON-387], [JACKSON-430] @JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY, property="@classAttr1") @@ -201,4 +214,13 @@ public void testJackson430() throws Exception assertNotNull(mc2.params); assertEquals(1, mc2.params.size()); } + + public void testValueWithMoreGenericParameters() throws Exception + { + WrappedContainerWithField wrappedContainerWithField = new WrappedContainerWithField(); + wrappedContainerWithField.animalContainer = new ContainerWithTwoAnimals(new Dog("d1",1), new Dog("d2",2)); + new ObjectMapper().writeValueAsString(wrappedContainerWithField); + // line above throws exception due to JACKSON-543 + } + } From f2831a0fe45e53e63c74d7aa7384d2cf492d47eb Mon Sep 17 00:00:00 2001 From: Alex Heneveld Date: Sat, 13 Sep 2014 01:14:17 +0100 Subject: [PATCH 2/2] workaround for JACKSON-543 --- .../com/fasterxml/jackson/databind/type/TypeBindings.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java b/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java index 9a92af2f6c..4b9d8103c9 100644 --- a/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java +++ b/src/main/java/com/fasterxml/jackson/databind/type/TypeBindings.java @@ -299,7 +299,9 @@ protected void _resolveBindings(Type t) } _addPlaceholder(name); // to prevent infinite loops - if (typeParams != null) { + if (typeParams != null && typeParams.length > i) { + // NB: due to JACKSON-543 typeParams might be totally unrelated; + // but for now we assume they are the same, with just a check to avoid NPE _bindings.put(name, typeParams[i]); } else { _bindings.put(name, _typeFactory._constructType(varType, this));