2
2
3
3
import java .io .*;
4
4
import java .lang .reflect .Array ;
5
+ import java .util .List ;
5
6
6
7
import org .junit .Assert ;
7
8
8
9
import com .fasterxml .jackson .annotation .JsonCreator ;
9
10
import com .fasterxml .jackson .annotation .JsonProperty ;
10
11
import com .fasterxml .jackson .core .*;
11
12
import com .fasterxml .jackson .databind .*;
13
+ import com .fasterxml .jackson .databind .JsonMappingException .Reference ;
14
+ import com .fasterxml .jackson .databind .exc .MismatchedInputException ;
12
15
13
16
/**
14
17
* Unit tests for verifying handling of simple basic non-structured
@@ -102,6 +105,14 @@ static class WrappersBean
102
105
public Double doubleValue ;
103
106
}
104
107
108
+ // [databind#2101]
109
+ static class PrimitiveCreatorBean
110
+ {
111
+ @ JsonCreator
112
+ public PrimitiveCreatorBean (@ JsonProperty (value ="a" ,required =true ) int a ,
113
+ @ JsonProperty (value ="b" ,required =true ) int b ) { }
114
+ }
115
+
105
116
private final ObjectMapper MAPPER = new ObjectMapper ();
106
117
107
118
/*
@@ -220,19 +231,18 @@ public void testCharacterWrapper() throws Exception
220
231
final CharacterWrapperBean wrapper = MAPPER .readValue ("{\" v\" :null}" , CharacterWrapperBean .class );
221
232
assertNotNull (wrapper );
222
233
assertNull (wrapper .getV ());
223
-
224
- final ObjectMapper mapper = new ObjectMapper ();
225
- mapper .enable (DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES );
234
+
226
235
try {
227
- mapper .readValue ("{\" v\" :null}" , CharacterBean .class );
236
+ MAPPER .readerFor (CharacterBean .class )
237
+ .with (DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES )
238
+ .readValue ("{\" v\" :null}" );
228
239
fail ("Attempting to deserialize a 'null' JSON reference into a 'char' property did not throw an exception" );
229
- } catch (JsonMappingException e ) {
240
+ } catch (MismatchedInputException e ) {
230
241
verifyException (e , "cannot map `null`" );
231
- //Exception thrown as required
232
242
}
233
-
234
- mapper . disable (DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES );
235
- final CharacterBean charBean = MAPPER .readValue ("{\" v\" :null}" , CharacterBean . class );
243
+ final CharacterBean charBean = MAPPER . readerFor ( CharacterBean . class )
244
+ . without (DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES )
245
+ .readValue ("{\" v\" :null}" );
236
246
assertNotNull (wrapper );
237
247
assertEquals ('\u0000' , charBean .getV ());
238
248
}
@@ -272,7 +282,7 @@ public void testIntPrimitive() throws Exception
272
282
try {
273
283
mapper .readValue ("{\" v\" :[3]}" , IntBean .class );
274
284
fail ("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled" );
275
- } catch (JsonMappingException exp ) {
285
+ } catch (MismatchedInputException exp ) {
276
286
//Correctly threw exception
277
287
}
278
288
@@ -287,7 +297,7 @@ public void testIntPrimitive() throws Exception
287
297
try {
288
298
mapper .readValue ("[{\" v\" :[3,3]}]" , IntBean .class );
289
299
fail ("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled" );
290
- } catch (JsonMappingException exp ) {
300
+ } catch (MismatchedInputException exp ) {
291
301
//threw exception as required
292
302
}
293
303
@@ -300,7 +310,7 @@ public void testIntPrimitive() throws Exception
300
310
assertEquals (1 , array .length );
301
311
assertEquals (0 , array [0 ]);
302
312
}
303
-
313
+
304
314
public void testLongWrapper () throws Exception
305
315
{
306
316
Long result = MAPPER .readValue ("12345678901" , Long .class );
@@ -335,7 +345,7 @@ public void testLongPrimitive() throws Exception
335
345
try {
336
346
mapper .readValue ("{\" v\" :[3]}" , LongBean .class );
337
347
fail ("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled" );
338
- } catch (JsonMappingException exp ) {
348
+ } catch (MismatchedInputException exp ) {
339
349
//Correctly threw exception
340
350
}
341
351
@@ -350,7 +360,7 @@ public void testLongPrimitive() throws Exception
350
360
try {
351
361
mapper .readValue ("[{\" v\" :[3,3]}]" , LongBean .class );
352
362
fail ("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled" );
353
- } catch (JsonMappingException exp ) {
363
+ } catch (MismatchedInputException exp ) {
354
364
//threw exception as required
355
365
}
356
366
@@ -473,7 +483,7 @@ public void testDoubleAsArray() throws Exception
473
483
try {
474
484
mapper .readValue ("[{\" v\" :[" + value + "," + value + "]}]" , DoubleBean .class );
475
485
fail ("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled" );
476
- } catch (JsonMappingException exp ) {
486
+ } catch (MismatchedInputException exp ) {
477
487
//threw exception as required
478
488
}
479
489
@@ -541,7 +551,7 @@ private void _testEmptyToNullCoercion(Class<?> primType, Object emptyValue) thro
541
551
intR .with (DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES )
542
552
.readValue ("\" \" " );
543
553
fail ("Should not have passed" );
544
- } catch (JsonMappingException e ) {
554
+ } catch (MismatchedInputException e ) {
545
555
verifyException (e , "Cannot coerce empty String" );
546
556
}
547
557
}
@@ -599,7 +609,6 @@ public void testSequenceOfInts() throws Exception
599
609
jp .close ();
600
610
}
601
611
602
-
603
612
/*
604
613
/**********************************************************
605
614
/* Empty String coercion, handling
@@ -712,56 +721,85 @@ public void testNullForPrimitives() throws IOException
712
721
try {
713
722
reader .readValue ("{\" booleanValue\" :null}" );
714
723
fail ("Expected failure for boolean + null" );
715
- } catch (JsonMappingException e ) {
724
+ } catch (MismatchedInputException e ) {
716
725
verifyException (e , "Cannot map `null` into type boolean" );
726
+ verifyPath (e , "booleanValue" );
717
727
}
718
728
// byte/char/short/int/long
719
729
try {
720
730
reader .readValue ("{\" byteValue\" :null}" );
721
731
fail ("Expected failure for byte + null" );
722
- } catch (JsonMappingException e ) {
732
+ } catch (MismatchedInputException e ) {
723
733
verifyException (e , "Cannot map `null` into type byte" );
734
+ verifyPath (e , "byteValue" );
724
735
}
725
736
try {
726
737
reader .readValue ("{\" charValue\" :null}" );
727
738
fail ("Expected failure for char + null" );
728
- } catch (JsonMappingException e ) {
739
+ } catch (MismatchedInputException e ) {
729
740
verifyException (e , "Cannot map `null` into type char" );
741
+ verifyPath (e , "charValue" );
730
742
}
731
743
try {
732
744
reader .readValue ("{\" shortValue\" :null}" );
733
745
fail ("Expected failure for short + null" );
734
- } catch (JsonMappingException e ) {
746
+ } catch (MismatchedInputException e ) {
735
747
verifyException (e , "Cannot map `null` into type short" );
748
+ verifyPath (e , "shortValue" );
736
749
}
737
750
try {
738
751
reader .readValue ("{\" intValue\" :null}" );
739
752
fail ("Expected failure for int + null" );
740
- } catch (JsonMappingException e ) {
753
+ } catch (MismatchedInputException e ) {
741
754
verifyException (e , "Cannot map `null` into type int" );
755
+ verifyPath (e , "intValue" );
742
756
}
743
757
try {
744
758
reader .readValue ("{\" longValue\" :null}" );
745
759
fail ("Expected failure for long + null" );
746
- } catch (JsonMappingException e ) {
760
+ } catch (MismatchedInputException e ) {
747
761
verifyException (e , "Cannot map `null` into type long" );
762
+ verifyPath (e , "longValue" );
748
763
}
749
764
750
765
// float/double
751
766
try {
752
767
reader .readValue ("{\" floatValue\" :null}" );
753
768
fail ("Expected failure for float + null" );
754
- } catch (JsonMappingException e ) {
769
+ } catch (MismatchedInputException e ) {
755
770
verifyException (e , "Cannot map `null` into type float" );
771
+ verifyPath (e , "floatValue" );
756
772
}
757
773
try {
758
774
reader .readValue ("{\" doubleValue\" :null}" );
759
775
fail ("Expected failure for double + null" );
760
- } catch (JsonMappingException e ) {
776
+ } catch (MismatchedInputException e ) {
761
777
verifyException (e , "Cannot map `null` into type double" );
778
+ verifyPath (e , "doubleValue" );
762
779
}
763
780
}
764
781
782
+ // [databind#2101]
783
+ public void testNullForPrimitivesViaCreator () throws IOException
784
+ {
785
+ try {
786
+ /*PrimitiveCreatorBean bean =*/ MAPPER
787
+ .readerFor (PrimitiveCreatorBean .class )
788
+ .with (DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES )
789
+ .readValue (aposToQuotes ("{'a': null}" ));
790
+ fail ("Expected failure for `int` and `null`" );
791
+ } catch (MismatchedInputException e ) {
792
+ verifyException (e , "Cannot map `null` into type int" );
793
+ verifyPath (e , "a" );
794
+ }
795
+ }
796
+
797
+ private void verifyPath (MismatchedInputException e , String propName ) {
798
+ final List <Reference > path = e .getPath ();
799
+ assertEquals (1 , path .size ());
800
+ assertEquals (propName , path .get (0 ).getFieldName ());
801
+ }
802
+
765
803
public void testNullForPrimitiveArrays () throws IOException
766
804
{
767
805
_testNullForPrimitiveArrays (boolean [].class , Boolean .FALSE );
0 commit comments