4
4
import java .util .*;
5
5
import java .util .concurrent .ArrayBlockingQueue ;
6
6
7
+ import com .fasterxml .jackson .annotation .JsonProperty ;
8
+
7
9
import com .fasterxml .jackson .core .*;
8
10
import com .fasterxml .jackson .core .type .TypeReference ;
11
+
9
12
import com .fasterxml .jackson .databind .*;
10
13
import com .fasterxml .jackson .databind .annotation .JsonDeserialize ;
11
14
import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
12
15
import com .fasterxml .jackson .databind .exc .MismatchedInputException ;
16
+ import com .fasterxml .jackson .databind .module .SimpleModule ;
13
17
14
18
@ SuppressWarnings ("serial" )
15
19
public class CollectionDeserTest
@@ -65,7 +69,7 @@ static class KeyListBean {
65
69
public List <Key > keys ;
66
70
}
67
71
68
- // [Issue #828]
72
+ // [databind #828]
69
73
@ JsonDeserialize (using =SomeObjectDeserializer .class )
70
74
static class SomeObject {}
71
75
@@ -79,6 +83,26 @@ public SomeObject deserialize(JsonParser p, DeserializationContext ctxt)
79
83
}
80
84
}
81
85
86
+ // [databind#3068]: Exception wrapping (or not)
87
+ static class MyContainerModel {
88
+ @ JsonProperty ("processor-id" )
89
+ public String id = "123" ;
90
+ }
91
+
92
+ static class MyJobModel {
93
+ public Map <String , MyContainerModel > containers = Collections .singletonMap ("key" ,
94
+ new MyContainerModel ());
95
+ public int maxChangeLogStreamPartitions = 13 ;
96
+ }
97
+
98
+ static class CustomException extends RuntimeException {
99
+ private static final long serialVersionUID = 1L ;
100
+
101
+ public CustomException (String s ) {
102
+ super (s );
103
+ }
104
+ }
105
+
82
106
/*
83
107
/**********************************************************
84
108
/* Test methods
@@ -272,24 +296,24 @@ public void testArrayIndexForExceptions() throws Exception
272
296
// for [databind#828]
273
297
public void testWrapExceptions () throws Exception
274
298
{
275
- ObjectMapper mapper = jsonMapperBuilder ()
276
- .enable ( DeserializationFeature . WRAP_EXCEPTIONS )
277
- .build ( );
299
+ final ObjectReader wrappingReader = MAPPER
300
+ .readerFor ( new TypeReference < List < SomeObject >>() {} )
301
+ .with ( DeserializationFeature . WRAP_EXCEPTIONS );
278
302
279
303
try {
280
- mapper .readValue ("[{}]" , new TypeReference < List < SomeObject >>() {} );
304
+ wrappingReader .readValue ("[{}]" );
281
305
} catch (JsonMappingException exc ) {
282
306
assertEquals ("I want to catch this exception" , exc .getOriginalMessage ());
283
307
} catch (RuntimeException exc ) {
284
308
fail ("The RuntimeException should have been wrapped with a JsonMappingException." );
285
309
}
286
310
287
- ObjectMapper mapperNoWrap = jsonMapperBuilder ()
288
- .disable ( DeserializationFeature . WRAP_EXCEPTIONS )
289
- .build ( );
311
+ final ObjectReader noWrapReader = MAPPER
312
+ .readerFor ( new TypeReference < List < SomeObject >>() {} )
313
+ .without ( DeserializationFeature . WRAP_EXCEPTIONS );
290
314
291
315
try {
292
- mapperNoWrap .readValue ("[{}]" , new TypeReference < List < SomeObject >>() {} );
316
+ noWrapReader .readValue ("[{}]" );
293
317
} catch (JsonMappingException exc ) {
294
318
fail ("It should not have wrapped the RuntimeException." );
295
319
} catch (RuntimeException exc ) {
@@ -310,4 +334,48 @@ public void testAbstractListAndSet() throws Exception
310
334
assertEquals (2 , set .values .size ());
311
335
assertEquals (HashSet .class , set .values .getClass ());
312
336
}
337
+
338
+ // for [databind#3068]
339
+ public void testWrapExceptions3068 () throws Exception
340
+ {
341
+ final SimpleModule module = new SimpleModule ("SimpleModule" , Version .unknownVersion ())
342
+ .addDeserializer (MyContainerModel .class ,
343
+ new JsonDeserializer <MyContainerModel >() {
344
+ @ Override
345
+ public MyContainerModel deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException {
346
+ throw new CustomException ("Custom message" );
347
+ }
348
+ });
349
+
350
+ final ObjectMapper mapper = jsonMapperBuilder ()
351
+ .addModule (module )
352
+ .build ();
353
+ final String json = mapper .writeValueAsString (new MyJobModel ());
354
+
355
+ // First, verify NO wrapping:
356
+ try {
357
+ mapper .readerFor (MyJobModel .class )
358
+ .without (DeserializationFeature .WRAP_EXCEPTIONS )
359
+ .readValue (json );
360
+ fail ("Should not pass" );
361
+ } catch (CustomException e ) {
362
+ verifyException (e , "Custom message" );
363
+ } catch (JacksonException e ) {
364
+ fail ("Should not have wrapped exception, got: " +e );
365
+ }
366
+
367
+ // and then wrapping
368
+ try {
369
+ mapper .readerFor (MyJobModel .class )
370
+ .with (DeserializationFeature .WRAP_EXCEPTIONS )
371
+ .readValue (json );
372
+ fail ("Should not pass" );
373
+ } catch (JacksonException e ) {
374
+ verifyException (e , "Custom message" );
375
+ assertEquals (JsonMappingException .class , e .getClass ());
376
+ Throwable rootC = e .getCause ();
377
+ assertNotNull (rootC );
378
+ assertEquals (CustomException .class , rootC .getClass ());
379
+ }
380
+ }
313
381
}
0 commit comments