@@ -190,6 +190,17 @@ public void setList(List<?> l) {
190
190
}
191
191
}
192
192
193
+ // for [databind#2553]
194
+ @ SuppressWarnings ("rawtypes" )
195
+ static class List2553 {
196
+ @ JsonDeserialize (contentAs = Item2553 .class )
197
+ public List items ;
198
+ }
199
+
200
+ static class Item2553 {
201
+ public String name ;
202
+ }
203
+
193
204
final static class InvalidContentClass
194
205
{
195
206
/* Such annotation not allowed, since it makes no sense;
@@ -230,10 +241,11 @@ public void setMap(Map<Object,Object> m)
230
241
/**********************************************************
231
242
*/
232
243
244
+ private final ObjectMapper MAPPER = newJsonMapper ();
245
+
233
246
public void testOverrideClassValid () throws Exception
234
247
{
235
- ObjectMapper m = new ObjectMapper ();
236
- CollectionHolder result = m .readValue
248
+ CollectionHolder result = MAPPER .readValue
237
249
("{ \" strings\" : [ \" test\" ] }" , CollectionHolder .class );
238
250
239
251
Collection <String > strs = result ._strings ;
@@ -244,9 +256,8 @@ public void testOverrideClassValid() throws Exception
244
256
245
257
public void testOverrideMapValid () throws Exception
246
258
{
247
- ObjectMapper m = new ObjectMapper ();
248
259
// note: expecting conversion from number to String, as well
249
- MapHolder result = m .readValue
260
+ MapHolder result = MAPPER .readValue
250
261
("{ \" strings\" : { \" a\" : 3 } }" , MapHolder .class );
251
262
252
263
Map <String ,String > strs = result ._data ;
@@ -258,8 +269,7 @@ public void testOverrideMapValid() throws Exception
258
269
259
270
public void testOverrideArrayClass () throws Exception
260
271
{
261
- ObjectMapper m = new ObjectMapper ();
262
- ArrayHolder result = m .readValue
272
+ ArrayHolder result = MAPPER .readValue
263
273
("{ \" strings\" : [ \" test\" ] }" , ArrayHolder .class );
264
274
265
275
String [] strs = result ._strings ;
@@ -272,7 +282,7 @@ public void testOverrideClassInvalid() throws Exception
272
282
{
273
283
// should fail due to incompatible Annotation
274
284
try {
275
- BrokenCollectionHolder result = new ObjectMapper () .readValue
285
+ BrokenCollectionHolder result = MAPPER .readValue
276
286
("{ \" strings\" : [ ] }" , BrokenCollectionHolder .class );
277
287
fail ("Expected a failure, but got results: " +result );
278
288
} catch (JsonMappingException jme ) {
@@ -288,21 +298,21 @@ public void testOverrideClassInvalid() throws Exception
288
298
289
299
public void testRootInterfaceAs () throws Exception
290
300
{
291
- RootInterface value = new ObjectMapper () .readValue ("{\" a\" :\" abc\" }" , RootInterface .class );
301
+ RootInterface value = MAPPER .readValue ("{\" a\" :\" abc\" }" , RootInterface .class );
292
302
assertTrue (value instanceof RootInterfaceImpl );
293
303
assertEquals ("abc" , value .getA ());
294
304
}
295
305
296
306
public void testRootInterfaceUsing () throws Exception
297
307
{
298
- RootString value = new ObjectMapper () .readValue ("\" xxx\" " , RootString .class );
308
+ RootString value = MAPPER .readValue ("\" xxx\" " , RootString .class );
299
309
assertTrue (value instanceof RootString );
300
310
assertEquals ("xxx" , value .contents ());
301
311
}
302
312
303
313
public void testRootListAs () throws Exception
304
314
{
305
- RootMap value = new ObjectMapper () .readValue ("{\" a\" :\" b\" }" , RootMap .class );
315
+ RootMap value = MAPPER .readValue ("{\" a\" :\" b\" }" , RootMap .class );
306
316
assertEquals (1 , value .size ());
307
317
Object v2 = value .get ("a" );
308
318
assertEquals (RootStringImpl .class , v2 .getClass ());
@@ -311,7 +321,7 @@ public void testRootListAs() throws Exception
311
321
312
322
public void testRootMapAs () throws Exception
313
323
{
314
- RootList value = new ObjectMapper () .readValue ("[ \" c\" ]" , RootList .class );
324
+ RootList value = MAPPER .readValue ("[ \" c\" ]" , RootList .class );
315
325
assertEquals (1 , value .size ());
316
326
Object v2 = value .get (0 );
317
327
assertEquals (RootStringImpl .class , v2 .getClass ());
@@ -327,8 +337,7 @@ public void testRootMapAs() throws Exception
327
337
@ SuppressWarnings ("unchecked" )
328
338
public void testOverrideKeyClassValid () throws Exception
329
339
{
330
- ObjectMapper m = new ObjectMapper ();
331
- MapKeyHolder result = m .readValue ("{ \" map\" : { \" xxx\" : \" yyy\" } }" , MapKeyHolder .class );
340
+ MapKeyHolder result = MAPPER .readValue ("{ \" map\" : { \" xxx\" : \" yyy\" } }" , MapKeyHolder .class );
332
341
Map <StringWrapper , String > map = (Map <StringWrapper ,String >)(Map <?,?>)result ._map ;
333
342
assertEquals (1 , map .size ());
334
343
Map .Entry <StringWrapper , String > en = map .entrySet ().iterator ().next ();
@@ -343,7 +352,7 @@ public void testOverrideKeyClassInvalid() throws Exception
343
352
{
344
353
// should fail due to incompatible Annotation
345
354
try {
346
- BrokenMapKeyHolder result = new ObjectMapper () .readValue
355
+ BrokenMapKeyHolder result = MAPPER .readValue
347
356
("{ \" 123\" : \" xxx\" }" , BrokenMapKeyHolder .class );
348
357
fail ("Expected a failure, but got results: " +result );
349
358
} catch (JsonMappingException jme ) {
@@ -358,10 +367,9 @@ public void testOverrideKeyClassInvalid() throws Exception
358
367
*/
359
368
360
369
@ SuppressWarnings ("unchecked" )
361
- public void testOverrideContentClassValid () throws Exception
370
+ public void testOverrideContentClassValid () throws Exception
362
371
{
363
- ObjectMapper m = new ObjectMapper ();
364
- ListContentHolder result = m .readValue ("{ \" list\" : [ \" abc\" ] }" , ListContentHolder .class );
372
+ ListContentHolder result = MAPPER .readValue ("{ \" list\" : [ \" abc\" ] }" , ListContentHolder .class );
365
373
List <StringWrapper > list = (List <StringWrapper >)result ._list ;
366
374
assertEquals (1 , list .size ());
367
375
Object value = list .get (0 );
@@ -371,8 +379,7 @@ public void testOverrideContentClassValid() throws Exception
371
379
372
380
public void testOverrideArrayContents () throws Exception
373
381
{
374
- ObjectMapper m = new ObjectMapper ();
375
- ArrayContentHolder result = m .readValue ("{ \" data\" : [ 1, 2, 3 ] }" ,
382
+ ArrayContentHolder result = MAPPER .readValue ("{ \" data\" : [ 1, 2, 3 ] }" ,
376
383
ArrayContentHolder .class );
377
384
Object [] data = result ._data ;
378
385
assertEquals (3 , data .length );
@@ -384,13 +391,22 @@ public void testOverrideArrayContents() throws Exception
384
391
385
392
public void testOverrideMapContents () throws Exception
386
393
{
387
- ObjectMapper m = new ObjectMapper ();
388
- MapContentHolder result = m .readValue ("{ \" map\" : { \" a\" : 9 } }" ,
394
+ MapContentHolder result = MAPPER .readValue ("{ \" map\" : { \" a\" : 9 } }" ,
389
395
MapContentHolder .class );
390
396
Map <Object ,Object > map = result ._map ;
391
397
assertEquals (1 , map .size ());
392
398
Object ob = map .values ().iterator ().next ();
393
399
assertEquals (Integer .class , ob .getClass ());
394
400
assertEquals (Integer .valueOf (9 ), ob );
395
401
}
402
+
403
+ // [databind#2553]
404
+ public void testRawListTypeContentAs () throws Exception
405
+ {
406
+ List2553 list = MAPPER .readValue ("{\" items\" : [{\" name\" :\" item1\" }]}" , List2553 .class );
407
+ assertEquals (1 , list .items .size ());
408
+ Object value = list .items .get (0 );
409
+ assertEquals (Item2553 .class , value .getClass ());
410
+ assertEquals ("item1" , ((Item2553 ) value ).name );
411
+ }
396
412
}
0 commit comments