Skip to content

Commit cab130e

Browse files
committed
Add more test cases for #1604
1 parent 2c4b2dd commit cab130e

File tree

3 files changed

+147
-10
lines changed

3 files changed

+147
-10
lines changed

src/test/java/com/fasterxml/jackson/databind/type/TestTypeFactory.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,15 +293,9 @@ public void testCollectionTypesRefined()
293293
assertEquals(Long.class, subtype.getContentType().getRawClass());
294294

295295
// but with refinement, should have non-null super class
296-
// 20-Oct-2015, tatu: For now refinement does not faithfully replicate the
297-
// structure, it only retains most important information. Here it means
298-
// that actually existing super-classes are skipped, and only original
299-
// type is linked as expected
300-
/*
301296
JavaType superType = subtype.getSuperClass();
302297
assertNotNull(superType);
303298
assertEquals(AbstractList.class, superType.getRawClass());
304-
*/
305299
}
306300

307301
/*
@@ -372,15 +366,13 @@ public void testMapTypesRefined()
372366
// that actually existing super-classes are skipped, and only original
373367
// type is linked as expected
374368

375-
/*
376369
JavaType superType = subtype.getSuperClass();
377370
assertNotNull(superType);
378371
assertEquals(HashMap.class, superType.getRawClass());
379372
// which also should have proper typing
380373
assertEquals(String.class, superType.getKeyType().getRawClass());
381374
assertEquals(List.class, superType.getContentType().getRawClass());
382375
assertEquals(Integer.class, superType.getContentType().getContentType().getRawClass());
383-
*/
384376
}
385377

386378
public void testMapTypesRaw()

src/test/java/com/fasterxml/jackson/failing/NestedTypes1604Test.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ public T getData() {
2222
public static <T> Data<List<T>> of(List<T> data) {
2323
return new DataList<>(data);
2424
}
25+
26+
public static <T> Data<List<T>> ofRefined(List<T> data) {
27+
return new RefinedDataList<>(data);
28+
}
29+
30+
public static <T> Data<List<T>> ofSneaky(List<T> data) {
31+
return new SneakyDataList<String,T>(data);
32+
}
2533
}
2634

2735
public static class DataList<T> extends Data<List<T>> {
@@ -30,6 +38,18 @@ public DataList(List<T> data) {
3038
}
3139
}
3240

41+
public static class RefinedDataList<T> extends Data<List<T>> {
42+
public RefinedDataList(List<T> data) {
43+
super(data);
44+
}
45+
}
46+
47+
public static class SneakyDataList<BOGUS,T> extends Data<List<T>> {
48+
public SneakyDataList(List<T> data) {
49+
super(data);
50+
}
51+
}
52+
3353
public static class Inner {
3454
private int index;
3555

@@ -66,9 +86,10 @@ public DataList<Inner> getInner() {
6686
}
6787
}
6888

69-
public void testIssue1604() throws Exception
89+
private final ObjectMapper objectMapper = new ObjectMapper();
90+
91+
public void testIssue1604Simple() throws Exception
7092
{
71-
final ObjectMapper objectMapper = new ObjectMapper();
7293
List<Inner> inners = new ArrayList<>();
7394
for (int i = 0; i < 2; i++) {
7495
inners.add(new Inner(i));
@@ -81,4 +102,26 @@ public void testIssue1604() throws Exception
81102
String json = objectMapper.writeValueAsString(badOuter);
82103
assertNotNull(json);
83104
}
105+
106+
public void testIssue1604Subtype() throws Exception
107+
{
108+
List<Inner> inners = new ArrayList<>();
109+
for (int i = 0; i < 2; i++) {
110+
inners.add(new Inner(i));
111+
}
112+
BadOuter badOuter = new BadOuter(Data.ofRefined(inners));
113+
String json = objectMapper.writeValueAsString(badOuter);
114+
assertNotNull(json);
115+
}
116+
117+
public void testIssue1604Sneaky() throws Exception
118+
{
119+
List<Inner> inners = new ArrayList<>();
120+
for (int i = 0; i < 2; i++) {
121+
inners.add(new Inner(i));
122+
}
123+
BadOuter badOuter = new BadOuter(Data.ofSneaky(inners));
124+
String json = objectMapper.writeValueAsString(badOuter);
125+
assertNotNull(json);
126+
}
84127
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.*;
4+
5+
import com.fasterxml.jackson.core.type.TypeReference;
6+
import com.fasterxml.jackson.databind.BaseMapTest;
7+
import com.fasterxml.jackson.databind.JavaType;
8+
import com.fasterxml.jackson.databind.type.TypeFactory;
9+
10+
// for [databind#1604]
11+
public class TestTypeFactory1604 extends BaseMapTest
12+
{
13+
static class Data1604<T> { }
14+
15+
static class DataList1604<T> extends Data1604<List<T>> {
16+
}
17+
18+
static class RefinedDataList1604<T> extends DataList1604<List<T>> {
19+
}
20+
21+
public static class SneakyDataList1604<BOGUS,T> extends Data1604<List<T>> {
22+
23+
}
24+
25+
static class TwoParam1604<KEY,VALUE> { }
26+
27+
static class SneakyTwoParam1604<V,K> extends TwoParam1604<K,List<V>> { }
28+
29+
public void testCustomTypesRefinedSimple()
30+
{
31+
TypeFactory tf = newTypeFactory();
32+
JavaType type = tf.constructType(new TypeReference<Data1604<Long>>() { });
33+
assertEquals(Data1604.class, type.getRawClass());
34+
35+
JavaType subtype = tf.constructSpecializedType(type, DataList1604.class);
36+
assertEquals(DataList1604.class, subtype.getRawClass());
37+
assertEquals(1, subtype.containedTypeCount());
38+
39+
// should properly resolve type parameter:
40+
JavaType paramType = subtype.containedType(0);
41+
assertEquals(Long.class, paramType.getRawClass());
42+
}
43+
44+
public void testCustomTypesRefinedNested()
45+
{
46+
TypeFactory tf = newTypeFactory();
47+
JavaType type = tf.constructType(new TypeReference<Data1604<Long>>() { });
48+
assertEquals(Data1604.class, type.getRawClass());
49+
50+
JavaType subtype = tf.constructSpecializedType(type, RefinedDataList1604.class);
51+
assertEquals(RefinedDataList1604.class, subtype.getRawClass());
52+
assertEquals(1, subtype.containedTypeCount());
53+
54+
// should properly resolve type parameter:
55+
JavaType paramType = subtype.containedType(0);
56+
assertEquals(Long.class, paramType.getRawClass());
57+
58+
// and have correct parent too
59+
assertEquals(DataList1604.class, subtype.getSuperClass().getRawClass());
60+
}
61+
62+
public void testCustomTypesRefinedSneaky()
63+
{
64+
TypeFactory tf = newTypeFactory();
65+
JavaType type = tf.constructType(new TypeReference<Data1604<Long>>() { });
66+
assertEquals(Data1604.class, type.getRawClass());
67+
68+
JavaType subtype = tf.constructSpecializedType(type, SneakyDataList1604.class);
69+
assertEquals(SneakyDataList1604.class, subtype.getRawClass());
70+
assertEquals(1, subtype.containedTypeCount());
71+
72+
// should properly resolve type parameter:
73+
JavaType paramType = subtype.containedType(0);
74+
assertEquals(Long.class, paramType.getRawClass());
75+
76+
// and have correct parent too
77+
assertEquals(Data1604.class, subtype.getSuperClass().getRawClass());
78+
}
79+
80+
public void testTwoParamSneakyCustom()
81+
{
82+
TypeFactory tf = newTypeFactory();
83+
JavaType type = tf.constructType(new TypeReference<TwoParam1604<String,Long>>() { });
84+
assertEquals(TwoParam1604.class, type.getRawClass());
85+
assertEquals(String.class, type.containedType(0).getRawClass());
86+
assertEquals(Long.class, type.containedType(1).getRawClass());
87+
88+
JavaType subtype = tf.constructSpecializedType(type, SneakyTwoParam1604.class);
89+
assertEquals(SneakyTwoParam1604.class, subtype.getRawClass());
90+
assertEquals(TwoParam1604.class, subtype.getSuperClass().getRawClass());
91+
assertEquals(2, subtype.containedTypeCount());
92+
93+
// should properly resolve type parameters despite sneaky switching
94+
JavaType first = subtype.containedType(0);
95+
assertEquals(List.class, first.getRawClass());
96+
assertEquals(1, first.containedTypeCount());
97+
assertEquals(Long.class, first.containedType(0).getRawClass());
98+
99+
JavaType second = subtype.containedType(1);
100+
assertEquals(String.class, second.getRawClass());
101+
}
102+
}

0 commit comments

Comments
 (0)