From e3ec9dc5a816ba1107816cd199f9be5e3f41d628 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Wed, 11 Sep 2019 16:33:32 -0700 Subject: [PATCH] Fix #2164 --- release-notes/CREDITS-2.x | 11 ++++-- release-notes/VERSION-2.x | 3 ++ .../databind/DeserializationContext.java | 8 ++-- .../deser/jdk/EnumDeserializationTest.java | 37 +++++++++++++++++++ 4 files changed, 53 insertions(+), 6 deletions(-) diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index 11ea4615e0..23023e68eb 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -880,14 +880,19 @@ Joe Barnett (josephlbarnett@github) ignored when creator properties are buffered (2.9.10) -Christoph Breitkopf (bokesan@github) - * Reported #2217: Suboptimal memory allocation in `TextNode.getBinaryValue()` +Yiqiu Huang (huangyq23@github + * Reported #2164: `FactoryBasedEnumDeserializer` does not respect + `DeserializationFeature.WRAP_EXCEPTIONS` (2.10.0) - + Alexander Saites (saites@github) * Reported #2189: `TreeTraversingParser` does not check int bounds (2.10.0) +Christoph Breitkopf (bokesan@github) + * Reported #2217: Suboptimal memory allocation in `TextNode.getBinaryValue()` + (2.10.0) + Pavel Chervakov (pacher@github) * Reported #2230: `WRITE_BIGDECIMAL_AS_PLAIN` is ignored if `@JsonFormat` is used (2.10.0) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 453b2f7712..079ea39c07 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -8,6 +8,9 @@ Project: jackson-databind #2149: Add `MapperFeature.ACCEPT_CASE_INSENSITIVE_VALUES` (suggested by Craig P) +#2164: `FactoryBasedEnumDeserializer` does not respect + `DeserializationFeature.WRAP_EXCEPTIONS` + (reported by Yiqiu H) #2309: READ_ENUMS_USING_TO_STRING doesn't support null values (reported, fix suggested by Ben A) #2357: Lack of path on MismatchedInputException diff --git a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java index 7491109b25..8bb983bf65 100644 --- a/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java +++ b/src/main/java/com/fasterxml/jackson/databind/DeserializationContext.java @@ -155,9 +155,7 @@ protected DeserializationContext(DeserializerFactory df) { protected DeserializationContext(DeserializerFactory df, DeserializerCache cache) { - if (df == null) { - throw new IllegalArgumentException("Cannot pass null DeserializerFactory"); - } + Objects.requireNonNull(df, "Cannot pass null DeserializerFactory"); _factory = df; if (cache == null) { cache = new DeserializerCache(); @@ -1090,6 +1088,10 @@ public Object handleInstantiationProblem(Class instClass, Object argument, } // 18-May-2016, tatu: Only wrap if not already a valid type to throw ClassUtil.throwIfIOE(t); + // [databind#2164]: but see if wrapping is desired + if (!isEnabled(DeserializationFeature.WRAP_EXCEPTIONS)) { + ClassUtil.throwIfRTE(t); + } throw instantiationException(instClass, t); } diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java index 9117ca9cbd..7cdcc38743 100644 --- a/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDeserializationTest.java @@ -14,6 +14,7 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.exc.InvalidFormatException; import com.fasterxml.jackson.databind.exc.MismatchedInputException; +import com.fasterxml.jackson.databind.exc.ValueInstantiationException; import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.module.SimpleModule; @@ -185,6 +186,16 @@ public static ObjectMapper setupObjectMapper(ObjectMapper mapper) { } } + // for [databind#2164] + public enum TestEnum2164 { + A, B; + + @JsonCreator + public static TestEnum2164 fromString(String input) { + throw new IllegalArgumentException("2164"); + } + } + // for [databind#2309] static enum Enum2309 { NON_NULL("NON_NULL"), @@ -551,6 +562,31 @@ public void testExceptionFromCustomEnumKeyDeserializer() throws Exception { } } + // [databind#2164] + public void testWrapExceptions() throws Exception + { + // By default, wrap: + try { + MAPPER.readerFor(TestEnum2164.class) + .readValue(quote("B")); + fail("Should not pass"); + } catch (ValueInstantiationException e) { + verifyException(e, "2164"); + } + + // But can disable: + try { + MAPPER.readerFor(TestEnum2164.class) + .without(DeserializationFeature.WRAP_EXCEPTIONS) + .readValue(quote("B")); + fail("Should not pass"); + } catch (JsonMappingException e) { + fail("Wrong exception, should not wrap, got: "+e); + } catch (IllegalArgumentException e) { + verifyException(e, "2164"); + } + } + // [databind#2309] public void testEnumToStringNull2309() throws Exception { @@ -559,4 +595,5 @@ public void testEnumToStringNull2309() throws Exception .readValue(quote("NON_NULL")); assertEquals(Enum2309.NON_NULL, value); } + }