diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java index 522855f6d7..0c6d43a08d 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/FactoryBasedEnumDeserializer.java @@ -134,6 +134,12 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx return _factory.callOnWith(_valueClass, value); } catch (Exception e) { Throwable t = ClassUtil.throwRootCauseIfIOE(e); + + if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL) && + t instanceof IllegalArgumentException) { + return null; + } + return ctxt.handleInstantiationProblem(_valueClass, value, 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 2a0bc19af9..07c74b32fb 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 @@ -116,6 +116,17 @@ static enum EnumWithDefaultAnnoAndConstructor { } } + static enum StrictEnumCreator { + A, B; + + @JsonCreator public static StrictEnumCreator fromId(String value) { + for (StrictEnumCreator e: values()) { + if (e.name().toLowerCase().equals(value)) return e; + } + throw new IllegalArgumentException(value); + } + } + // // public enum AnEnum { @@ -321,6 +332,16 @@ public void testAllowUnknownEnumValuesReadAsNull() throws Exception assertNull(reader.forType(TestEnum.class).readValue(" 4343 ")); } + // Ability to ignore unknown Enum values: + + public void testAllowUnknownEnumValuesReadAsNullWithCreatorMethod() throws Exception + { + // can not use shared mapper when changing configs... + ObjectReader reader = MAPPER.reader(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL); + assertNull(reader.forType(StrictEnumCreator.class).readValue("\"NO-SUCH-VALUE\"")); + assertNull(reader.forType(StrictEnumCreator.class).readValue(" 4343 ")); + } + public void testAllowUnknownEnumValuesForEnumSets() throws Exception { ObjectReader reader = MAPPER.reader(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);