Skip to content

Commit 5a98834

Browse files
authored
Add reproduction (unit test) for #4118 (#4128)
* add test * clean up
1 parent ab4721a commit 5a98834

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ public TypeBindings withUnboundVariable(String name)
217217
* Create a new instance with the same bindings as this object, except with
218218
* the given variable removed. This is used to create generic types that are
219219
* "partially raw", i.e. only have some variables bound.
220+
*
221+
* @since 2.16
220222
*/
221223
public TypeBindings withoutVariable(String name)
222224
{

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,8 +1706,8 @@ protected JavaType _fromParamType(ClassStack context, ParameterizedType ptype,
17061706
}
17071707
newBindings = TypeBindings.create(rawType, pt);
17081708

1709-
// jackson-databind#4118: Unbind any wildcards with a less specific upper bound than
1710-
// declared on the type variable
1709+
// [databind#4118] Unbind any wildcards with a less specific upper bound than
1710+
// declared on the type variable (since 2.16)
17111711
for (int i = 0; i < paramCount; ++i) {
17121712
if (args[i] instanceof WildcardType && !pt[i].hasGenericTypes()) {
17131713
TypeVariable<? extends Class<?>> typeVariable = rawType.getTypeParameters()[i];
Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fasterxml.jackson.databind.type;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
import com.fasterxml.jackson.annotation.JsonCreator;
@@ -11,19 +12,7 @@
1112

1213
public class RecursiveWildcardTest extends BaseMapTest
1314
{
14-
public void test() throws Exception
15-
{
16-
ObjectMapper mapper = newJsonMapper();
17-
18-
Tree<?> tree = mapper.readValue("[[[]]]", new TypeReference<Tree<?>>() {
19-
});
20-
21-
assertEquals(1, tree.children.size());
22-
assertEquals(1, tree.children.get(0).children.size());
23-
assertEquals(0, tree.children.get(0).children.get(0).children.size());
24-
}
25-
26-
public static class Tree<T extends Tree<?>> {
15+
static class Tree<T extends Tree<?>> {
2716

2817
final List<T> children;
2918

@@ -35,6 +24,63 @@ public Tree(List<T> children) {
3524
}
3625
this.children = children;
3726
}
27+
}
28+
29+
static class TestAttribute4118<T extends TestAttribute4118<?>> {
30+
31+
public List<T> attributes;
32+
33+
public TestAttribute4118() { }
34+
35+
public TestAttribute4118(List<T> attributes) {
36+
this.attributes = attributes;
37+
}
38+
}
39+
40+
static class TestObject4118 {
41+
42+
public List<TestAttribute4118<?>> attributes = new ArrayList<>();
43+
44+
public TestObject4118() { }
45+
46+
public TestObject4118(List<TestAttribute4118<?>> attributes) {
47+
this.attributes = attributes;
48+
}
49+
}
50+
51+
private final ObjectMapper MAPPER = newJsonMapper();
52+
53+
public void testRecursiveWildcard() throws Exception
54+
{
55+
Tree<?> tree = MAPPER.readValue("[[[]]]", new TypeReference<Tree<?>>() {
56+
});
57+
58+
assertEquals(1, tree.children.size());
59+
assertEquals(1, tree.children.get(0).children.size());
60+
assertEquals(0, tree.children.get(0).children.get(0).children.size());
61+
}
62+
63+
// for [databind#4118]
64+
public void testDeserWildcard4118() throws Exception
65+
{
66+
// Given
67+
TestAttribute4118 a = new TestAttribute4118(null);
68+
TestAttribute4118 b = new TestAttribute4118(_listOf(a));
69+
TestAttribute4118 c = new TestAttribute4118(_listOf(b));
70+
TestObject4118 test = new TestObject4118(_listOf(c));
71+
72+
String serialized = MAPPER.writeValueAsString(test);
73+
74+
// When
75+
TestObject4118 deserialized = MAPPER.readValue(serialized, TestObject4118.class);
76+
77+
// Then
78+
assertType(deserialized.attributes.get(0).attributes.get(0), TestAttribute4118.class);
79+
}
3880

81+
private <T> List<T> _listOf(T elem) {
82+
ArrayList<T> list = new ArrayList<>();
83+
list.add(elem);
84+
return list;
3985
}
4086
}

0 commit comments

Comments
 (0)