Skip to content

Commit 5256bfc

Browse files
committed
Add bit more testing, related to investigation of #2101 (although ultimately determined there is no bug)
1 parent 6abb1dd commit 5256bfc

File tree

2 files changed

+128
-25
lines changed

2 files changed

+128
-25
lines changed

src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java

+63-25
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import java.io.*;
44
import java.lang.reflect.Array;
5+
import java.util.List;
56

67
import org.junit.Assert;
78

89
import com.fasterxml.jackson.annotation.JsonCreator;
910
import com.fasterxml.jackson.annotation.JsonProperty;
1011
import com.fasterxml.jackson.core.*;
1112
import com.fasterxml.jackson.databind.*;
13+
import com.fasterxml.jackson.databind.JsonMappingException.Reference;
14+
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
1215

1316
/**
1417
* Unit tests for verifying handling of simple basic non-structured
@@ -102,6 +105,14 @@ static class WrappersBean
102105
public Double doubleValue;
103106
}
104107

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+
105116
private final ObjectMapper MAPPER = new ObjectMapper();
106117

107118
/*
@@ -220,19 +231,18 @@ public void testCharacterWrapper() throws Exception
220231
final CharacterWrapperBean wrapper = MAPPER.readValue("{\"v\":null}", CharacterWrapperBean.class);
221232
assertNotNull(wrapper);
222233
assertNull(wrapper.getV());
223-
224-
final ObjectMapper mapper = new ObjectMapper();
225-
mapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES);
234+
226235
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}");
228239
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) {
230241
verifyException(e, "cannot map `null`");
231-
//Exception thrown as required
232242
}
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}");
236246
assertNotNull(wrapper);
237247
assertEquals('\u0000', charBean.getV());
238248
}
@@ -272,7 +282,7 @@ public void testIntPrimitive() throws Exception
272282
try {
273283
mapper.readValue("{\"v\":[3]}", IntBean.class);
274284
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) {
276286
//Correctly threw exception
277287
}
278288

@@ -287,7 +297,7 @@ public void testIntPrimitive() throws Exception
287297
try {
288298
mapper.readValue("[{\"v\":[3,3]}]", IntBean.class);
289299
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) {
291301
//threw exception as required
292302
}
293303

@@ -300,7 +310,7 @@ public void testIntPrimitive() throws Exception
300310
assertEquals(1, array.length);
301311
assertEquals(0, array[0]);
302312
}
303-
313+
304314
public void testLongWrapper() throws Exception
305315
{
306316
Long result = MAPPER.readValue("12345678901", Long.class);
@@ -335,7 +345,7 @@ public void testLongPrimitive() throws Exception
335345
try {
336346
mapper.readValue("{\"v\":[3]}", LongBean.class);
337347
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) {
339349
//Correctly threw exception
340350
}
341351

@@ -350,7 +360,7 @@ public void testLongPrimitive() throws Exception
350360
try {
351361
mapper.readValue("[{\"v\":[3,3]}]", LongBean.class);
352362
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) {
354364
//threw exception as required
355365
}
356366

@@ -473,7 +483,7 @@ public void testDoubleAsArray() throws Exception
473483
try {
474484
mapper.readValue("[{\"v\":[" + value + "," + value + "]}]", DoubleBean.class);
475485
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) {
477487
//threw exception as required
478488
}
479489

@@ -541,7 +551,7 @@ private void _testEmptyToNullCoercion(Class<?> primType, Object emptyValue) thro
541551
intR.with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)
542552
.readValue("\"\"");
543553
fail("Should not have passed");
544-
} catch (JsonMappingException e) {
554+
} catch (MismatchedInputException e) {
545555
verifyException(e, "Cannot coerce empty String");
546556
}
547557
}
@@ -599,7 +609,6 @@ public void testSequenceOfInts() throws Exception
599609
jp.close();
600610
}
601611

602-
603612
/*
604613
/**********************************************************
605614
/* Empty String coercion, handling
@@ -712,56 +721,85 @@ public void testNullForPrimitives() throws IOException
712721
try {
713722
reader.readValue("{\"booleanValue\":null}");
714723
fail("Expected failure for boolean + null");
715-
} catch (JsonMappingException e) {
724+
} catch (MismatchedInputException e) {
716725
verifyException(e, "Cannot map `null` into type boolean");
726+
verifyPath(e, "booleanValue");
717727
}
718728
// byte/char/short/int/long
719729
try {
720730
reader.readValue("{\"byteValue\":null}");
721731
fail("Expected failure for byte + null");
722-
} catch (JsonMappingException e) {
732+
} catch (MismatchedInputException e) {
723733
verifyException(e, "Cannot map `null` into type byte");
734+
verifyPath(e, "byteValue");
724735
}
725736
try {
726737
reader.readValue("{\"charValue\":null}");
727738
fail("Expected failure for char + null");
728-
} catch (JsonMappingException e) {
739+
} catch (MismatchedInputException e) {
729740
verifyException(e, "Cannot map `null` into type char");
741+
verifyPath(e, "charValue");
730742
}
731743
try {
732744
reader.readValue("{\"shortValue\":null}");
733745
fail("Expected failure for short + null");
734-
} catch (JsonMappingException e) {
746+
} catch (MismatchedInputException e) {
735747
verifyException(e, "Cannot map `null` into type short");
748+
verifyPath(e, "shortValue");
736749
}
737750
try {
738751
reader.readValue("{\"intValue\":null}");
739752
fail("Expected failure for int + null");
740-
} catch (JsonMappingException e) {
753+
} catch (MismatchedInputException e) {
741754
verifyException(e, "Cannot map `null` into type int");
755+
verifyPath(e, "intValue");
742756
}
743757
try {
744758
reader.readValue("{\"longValue\":null}");
745759
fail("Expected failure for long + null");
746-
} catch (JsonMappingException e) {
760+
} catch (MismatchedInputException e) {
747761
verifyException(e, "Cannot map `null` into type long");
762+
verifyPath(e, "longValue");
748763
}
749764

750765
// float/double
751766
try {
752767
reader.readValue("{\"floatValue\":null}");
753768
fail("Expected failure for float + null");
754-
} catch (JsonMappingException e) {
769+
} catch (MismatchedInputException e) {
755770
verifyException(e, "Cannot map `null` into type float");
771+
verifyPath(e, "floatValue");
756772
}
757773
try {
758774
reader.readValue("{\"doubleValue\":null}");
759775
fail("Expected failure for double + null");
760-
} catch (JsonMappingException e) {
776+
} catch (MismatchedInputException e) {
761777
verifyException(e, "Cannot map `null` into type double");
778+
verifyPath(e, "doubleValue");
762779
}
763780
}
764781

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+
765803
public void testNullForPrimitiveArrays() throws IOException
766804
{
767805
_testNullForPrimitiveArrays(boolean[].class, Boolean.FALSE);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.fasterxml.jackson.failing;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
import com.fasterxml.jackson.databind.*;
7+
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
8+
9+
public class EnumAsIndexMapKey1877Test extends BaseMapTest
10+
{
11+
public enum Type {
12+
ANY,
13+
OTHER
14+
}
15+
16+
static class Container {
17+
private Type simpleType;
18+
private Map<Type, String> map;
19+
20+
public Type getSimpleType() {
21+
return simpleType;
22+
}
23+
24+
public void setSimpleType(Type simpleType) {
25+
this.simpleType= simpleType;
26+
}
27+
28+
public Map<Type, String> getMap() {
29+
return map;
30+
}
31+
32+
public void setMap(Map<Type, String> map) {
33+
this.map = map;
34+
}
35+
}
36+
// [databind#1877]
37+
public void testEnumAsIndexMapKey() throws Exception
38+
{
39+
ObjectMapper mapper = newObjectMapper();
40+
41+
Map<Type, String> map = new HashMap<>();
42+
map.put(Type.OTHER, "hello world");
43+
Container container = new Container();
44+
container.setSimpleType(Type.ANY);
45+
container.setMap(map);
46+
47+
String json = mapper
48+
.writer().with(SerializationFeature.WRITE_ENUMS_USING_INDEX)
49+
.writeValueAsString(container);
50+
51+
Container cont;
52+
try {
53+
cont = mapper
54+
.readerFor(Container.class)
55+
.readValue(json);
56+
} catch (JsonMappingException e) {
57+
throw e;
58+
}
59+
assertNotNull(cont);
60+
assertEquals(1, container.getMap().size());
61+
InvalidFormatException foo = null;
62+
63+
assertSame(Type.OTHER, container.getMap().keySet().iterator().next());
64+
}
65+
}

0 commit comments

Comments
 (0)