diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java index 66d9951d6d..64aea49133 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/JDKScalarsTest.java @@ -2,6 +2,7 @@ import java.io.*; import java.lang.reflect.Array; +import java.util.List; import org.junit.Assert; @@ -9,6 +10,8 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.JsonMappingException.Reference; +import com.fasterxml.jackson.databind.exc.MismatchedInputException; /** * Unit tests for verifying handling of simple basic non-structured @@ -102,6 +105,14 @@ static class WrappersBean public Double doubleValue; } + // [databind#2101] + static class PrimitiveCreatorBean + { + @JsonCreator + public PrimitiveCreatorBean(@JsonProperty(value="a",required=true) int a, + @JsonProperty(value="b",required=true) int b) { } + } + private final ObjectMapper MAPPER = new ObjectMapper(); /* @@ -220,19 +231,18 @@ public void testCharacterWrapper() throws Exception final CharacterWrapperBean wrapper = MAPPER.readValue("{\"v\":null}", CharacterWrapperBean.class); assertNotNull(wrapper); assertNull(wrapper.getV()); - - final ObjectMapper mapper = new ObjectMapper(); - mapper.enable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); + try { - mapper.readValue("{\"v\":null}", CharacterBean.class); + MAPPER.readerFor(CharacterBean.class) + .with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + .readValue("{\"v\":null}"); fail("Attempting to deserialize a 'null' JSON reference into a 'char' property did not throw an exception"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "cannot map `null`"); - //Exception thrown as required } - - mapper.disable(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES); - final CharacterBean charBean = MAPPER.readValue("{\"v\":null}", CharacterBean.class); + final CharacterBean charBean = MAPPER.readerFor(CharacterBean.class) + .without(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + .readValue("{\"v\":null}"); assertNotNull(wrapper); assertEquals('\u0000', charBean.getV()); } @@ -272,7 +282,7 @@ public void testIntPrimitive() throws Exception try { mapper.readValue("{\"v\":[3]}", IntBean.class); fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled"); - } catch (JsonMappingException exp) { + } catch (MismatchedInputException exp) { //Correctly threw exception } @@ -287,7 +297,7 @@ public void testIntPrimitive() throws Exception try { mapper.readValue("[{\"v\":[3,3]}]", IntBean.class); fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled"); - } catch (JsonMappingException exp) { + } catch (MismatchedInputException exp) { //threw exception as required } @@ -300,7 +310,7 @@ public void testIntPrimitive() throws Exception assertEquals(1, array.length); assertEquals(0, array[0]); } - + public void testLongWrapper() throws Exception { Long result = MAPPER.readValue("12345678901", Long.class); @@ -335,7 +345,7 @@ public void testLongPrimitive() throws Exception try { mapper.readValue("{\"v\":[3]}", LongBean.class); fail("Did not throw exception when reading a value from a single value array with the UNWRAP_SINGLE_VALUE_ARRAYS feature disabled"); - } catch (JsonMappingException exp) { + } catch (MismatchedInputException exp) { //Correctly threw exception } @@ -350,7 +360,7 @@ public void testLongPrimitive() throws Exception try { mapper.readValue("[{\"v\":[3,3]}]", LongBean.class); fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled"); - } catch (JsonMappingException exp) { + } catch (MismatchedInputException exp) { //threw exception as required } @@ -473,7 +483,7 @@ public void testDoubleAsArray() throws Exception try { mapper.readValue("[{\"v\":[" + value + "," + value + "]}]", DoubleBean.class); fail("Did not throw exception while reading a value from a multi value array with UNWRAP_SINGLE_VALUE_ARRAY feature enabled"); - } catch (JsonMappingException exp) { + } catch (MismatchedInputException exp) { //threw exception as required } @@ -541,7 +551,7 @@ private void _testEmptyToNullCoercion(Class primType, Object emptyValue) thro intR.with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) .readValue("\"\""); fail("Should not have passed"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot coerce empty String"); } } @@ -599,7 +609,6 @@ public void testSequenceOfInts() throws Exception jp.close(); } - /* /********************************************************** /* Empty String coercion, handling @@ -712,56 +721,85 @@ public void testNullForPrimitives() throws IOException try { reader.readValue("{\"booleanValue\":null}"); fail("Expected failure for boolean + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type boolean"); + verifyPath(e, "booleanValue"); } // byte/char/short/int/long try { reader.readValue("{\"byteValue\":null}"); fail("Expected failure for byte + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type byte"); + verifyPath(e, "byteValue"); } try { reader.readValue("{\"charValue\":null}"); fail("Expected failure for char + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type char"); + verifyPath(e, "charValue"); } try { reader.readValue("{\"shortValue\":null}"); fail("Expected failure for short + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type short"); + verifyPath(e, "shortValue"); } try { reader.readValue("{\"intValue\":null}"); fail("Expected failure for int + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type int"); + verifyPath(e, "intValue"); } try { reader.readValue("{\"longValue\":null}"); fail("Expected failure for long + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type long"); + verifyPath(e, "longValue"); } // float/double try { reader.readValue("{\"floatValue\":null}"); fail("Expected failure for float + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type float"); + verifyPath(e, "floatValue"); } try { reader.readValue("{\"doubleValue\":null}"); fail("Expected failure for double + null"); - } catch (JsonMappingException e) { + } catch (MismatchedInputException e) { verifyException(e, "Cannot map `null` into type double"); + verifyPath(e, "doubleValue"); } } + // [databind#2101] + public void testNullForPrimitivesViaCreator() throws IOException + { + try { + /*PrimitiveCreatorBean bean =*/ MAPPER + .readerFor(PrimitiveCreatorBean.class) + .with(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES) + .readValue(aposToQuotes("{'a': null}")); + fail("Expected failure for `int` and `null`"); + } catch (MismatchedInputException e) { + verifyException(e, "Cannot map `null` into type int"); + verifyPath(e, "a"); + } + } + + private void verifyPath(MismatchedInputException e, String propName) { + final List path = e.getPath(); + assertEquals(1, path.size()); + assertEquals(propName, path.get(0).getFieldName()); + } + public void testNullForPrimitiveArrays() throws IOException { _testNullForPrimitiveArrays(boolean[].class, Boolean.FALSE); diff --git a/src/test/java/com/fasterxml/jackson/failing/EnumAsIndexMapKey1877Test.java b/src/test/java/com/fasterxml/jackson/failing/EnumAsIndexMapKey1877Test.java new file mode 100644 index 0000000000..ebe12d31ce --- /dev/null +++ b/src/test/java/com/fasterxml/jackson/failing/EnumAsIndexMapKey1877Test.java @@ -0,0 +1,65 @@ +package com.fasterxml.jackson.failing; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.databind.*; +import com.fasterxml.jackson.databind.exc.InvalidFormatException; + +public class EnumAsIndexMapKey1877Test extends BaseMapTest +{ + public enum Type { + ANY, + OTHER + } + + static class Container { + private Type simpleType; + private Map map; + + public Type getSimpleType() { + return simpleType; + } + + public void setSimpleType(Type simpleType) { + this.simpleType= simpleType; + } + + public Map getMap() { + return map; + } + + public void setMap(Map map) { + this.map = map; + } + } + // [databind#1877] + public void testEnumAsIndexMapKey() throws Exception + { + ObjectMapper mapper = newObjectMapper(); + + Map map = new HashMap<>(); + map.put(Type.OTHER, "hello world"); + Container container = new Container(); + container.setSimpleType(Type.ANY); + container.setMap(map); + + String json = mapper + .writer().with(SerializationFeature.WRITE_ENUMS_USING_INDEX) + .writeValueAsString(container); + + Container cont; + try { + cont = mapper + .readerFor(Container.class) + .readValue(json); + } catch (JsonMappingException e) { + throw e; + } + assertNotNull(cont); + assertEquals(1, container.getMap().size()); + InvalidFormatException foo = null; + + assertSame(Type.OTHER, container.getMap().keySet().iterator().next()); + } +}