diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index 98ea7fba12..6dcc61c812 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -45,6 +45,8 @@ Project: jackson-databind #4394: Better Base64 support for `java.util.UUIDs` without padding (fix contributed by Jesper B) +#4403: Deserialization of unknown value for enums does not yield default enum value + (reported by @dominik-henning) #4416: Deprecate `JsonNode.asText(String)` (suggested by AndrĂ¡s P) - JUnit5 upgraded to 5.10.1 diff --git a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java index 108a635b97..68c2be07c6 100644 --- a/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java +++ b/src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java @@ -415,18 +415,25 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext // [databind#149]: Allow use of 'String' indexes as well -- unless prohibited (as per above) char c = name.charAt(0); if (c >= '0' && c <= '9') { - try { - int index = Integer.parseInt(name); - if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) { - return ctxt.handleWeirdStringValue(_enumClass(), name, + // [databind#4403]: cannot prevent "Stringified" numbers as Enum + // index yet (might need combination of "Does format have Numbers" + // (XML does not f.ex) and new `EnumFeature`. But can disallow "001" etc. + if (c == '0' && name.length() > 1) { + ; + } else { + try { + int index = Integer.parseInt(name); + if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) { + return ctxt.handleWeirdStringValue(_enumClass(), name, "value looks like quoted Enum index, but `MapperFeature.ALLOW_COERCION_OF_SCALARS` prevents use" - ); + ); + } + if (index >= 0 && index < _enumsByIndex.length) { + return _enumsByIndex[index]; + } + } catch (NumberFormatException e) { + // fine, ignore, was not an integer } - if (index >= 0 && index < _enumsByIndex.length) { - return _enumsByIndex[index]; - } - } catch (NumberFormatException e) { - // fine, ignore, was not an integer } } } diff --git a/src/test/java/com/fasterxml/jackson/failing/EnumDefaultRead4403Test.java b/src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDefaultRead4403Test.java similarity index 96% rename from src/test/java/com/fasterxml/jackson/failing/EnumDefaultRead4403Test.java rename to src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDefaultRead4403Test.java index b8be538601..212da30861 100644 --- a/src/test/java/com/fasterxml/jackson/failing/EnumDefaultRead4403Test.java +++ b/src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDefaultRead4403Test.java @@ -1,4 +1,4 @@ -package com.fasterxml.jackson.failing; +package com.fasterxml.jackson.databind.deser.enums; import org.junit.jupiter.api.Test;