11
11
import com .fasterxml .jackson .databind .*;
12
12
import com .fasterxml .jackson .databind .ser .BeanSerializerFactory ;
13
13
import com .fasterxml .jackson .databind .ser .ResolvableSerializer ;
14
- import com .fasterxml .jackson .databind .type .TypeFactory ;
15
14
16
15
public class TestWithGenerics extends BaseMapTest
17
16
{
@@ -45,7 +44,11 @@ static class ContainerWithField<T extends Animal> {
45
44
public ContainerWithField (T a ) { animal = a ; }
46
45
}
47
46
48
- // Beans for [JACKSON-387], [JACKSON-430]
47
+ static class WrappedContainerWithField {
48
+ public ContainerWithField <?> animalContainer ;
49
+ }
50
+
51
+ // Beans for [JACKSON-387], [JACKSON-430]
49
52
50
53
@ JsonTypeInfo (use =JsonTypeInfo .Id .CLASS , include =JsonTypeInfo .As .PROPERTY , property ="@classAttr1" )
51
54
static class MyClass {
@@ -114,17 +117,28 @@ protected JsonSerializer<Object> constructBeanSerializer(SerializerProvider prov
114
117
}
115
118
}
116
119
120
+ // [Issue#543]
121
+ static class ContainerWithTwoAnimals <U extends Animal ,V extends Animal > extends ContainerWithField <U > {
122
+ public V otherAnimal ;
123
+
124
+ public ContainerWithTwoAnimals (U a1 , V a2 ) {
125
+ super (a1 );
126
+ otherAnimal = a2 ;
127
+ }
128
+ }
117
129
118
130
/*
119
131
/**********************************************************
120
132
/* Unit tests
121
133
/**********************************************************
122
134
*/
123
135
136
+ private final ObjectMapper MAPPER = objectMapper ();
137
+
124
138
public void testWrapperWithGetter () throws Exception
125
139
{
126
140
Dog dog = new Dog ("Fluffy" , 3 );
127
- String json = new ObjectMapper () .writeValueAsString (new ContainerWithGetter <Animal >(dog ));
141
+ String json = MAPPER .writeValueAsString (new ContainerWithGetter <Animal >(dog ));
128
142
if (json .indexOf ("\" object-type\" :\" doggy\" " ) < 0 ) {
129
143
fail ("polymorphic type not kept, result == " +json +"; should contain 'object-type':'...'" );
130
144
}
@@ -133,7 +147,7 @@ public void testWrapperWithGetter() throws Exception
133
147
public void testWrapperWithField () throws Exception
134
148
{
135
149
Dog dog = new Dog ("Fluffy" , 3 );
136
- String json = new ObjectMapper () .writeValueAsString (new ContainerWithField <Animal >(dog ));
150
+ String json = MAPPER .writeValueAsString (new ContainerWithField <Animal >(dog ));
137
151
if (json .indexOf ("\" object-type\" :\" doggy\" " ) < 0 ) {
138
152
fail ("polymorphic type not kept, result == " +json +"; should contain 'object-type':'...'" );
139
153
}
@@ -143,8 +157,7 @@ public void testWrapperWithExplicitType() throws Exception
143
157
{
144
158
Dog dog = new Dog ("Fluffy" , 3 );
145
159
ContainerWithGetter <Animal > c2 = new ContainerWithGetter <Animal >(dog );
146
- ObjectMapper mapper = new ObjectMapper ();
147
- String json = mapper .writerWithType (TypeFactory .defaultInstance ().constructParametricType (ContainerWithGetter .class , Animal .class )).writeValueAsString (c2 );
160
+ String json = MAPPER .writerWithType (MAPPER .getTypeFactory ().constructParametricType (ContainerWithGetter .class , Animal .class )).writeValueAsString (c2 );
148
161
if (json .indexOf ("\" object-type\" :\" doggy\" " ) < 0 ) {
149
162
fail ("polymorphic type not kept, result == " +json +"; should contain 'object-type':'...'" );
150
163
}
@@ -201,4 +214,13 @@ public void testJackson430() throws Exception
201
214
assertNotNull (mc2 .params );
202
215
assertEquals (1 , mc2 .params .size ());
203
216
}
217
+
218
+ // [Issue#543]
219
+ public void testValueWithMoreGenericParameters () throws Exception
220
+ {
221
+ WrappedContainerWithField wrappedContainerWithField = new WrappedContainerWithField ();
222
+ wrappedContainerWithField .animalContainer = new ContainerWithTwoAnimals <Dog ,Dog >(new Dog ("d1" ,1 ), new Dog ("d2" ,2 ));
223
+ String json = MAPPER .writeValueAsString (wrappedContainerWithField );
224
+ assertNotNull (json );
225
+ }
204
226
}
0 commit comments