Skip to content

Commit ac9449d

Browse files
committed
Fix #4403: prevent use of zero-prefixed String as Enum index on deserialization
1 parent 683dbb0 commit ac9449d

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

release-notes/VERSION-2.x

+2
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ Project: jackson-databind
4545
#4394: Better Base64 support for `java.util.UUIDs`
4646
without padding
4747
(fix contributed by Jesper B)
48+
#4403: Deserialization of unknown value for enums does not yield default enum value
49+
(reported by @dominik-henning)
4850
#4416: Deprecate `JsonNode.asText(String)`
4951
(suggested by András P)
5052
- JUnit5 upgraded to 5.10.1

src/main/java/com/fasterxml/jackson/databind/deser/std/EnumDeserializer.java

+17-10
Original file line numberDiff line numberDiff line change
@@ -415,18 +415,25 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext
415415
// [databind#149]: Allow use of 'String' indexes as well -- unless prohibited (as per above)
416416
char c = name.charAt(0);
417417
if (c >= '0' && c <= '9') {
418-
try {
419-
int index = Integer.parseInt(name);
420-
if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
421-
return ctxt.handleWeirdStringValue(_enumClass(), name,
418+
// [databind#4403]: cannot prevent "Stringified" numbers as Enum
419+
// index yet (might need combination of "Does format have Numbers"
420+
// (XML does not f.ex) and new `EnumFeature`. But can disallow "001" etc.
421+
if (c == '0' && name.length() > 1) {
422+
;
423+
} else {
424+
try {
425+
int index = Integer.parseInt(name);
426+
if (!ctxt.isEnabled(MapperFeature.ALLOW_COERCION_OF_SCALARS)) {
427+
return ctxt.handleWeirdStringValue(_enumClass(), name,
422428
"value looks like quoted Enum index, but `MapperFeature.ALLOW_COERCION_OF_SCALARS` prevents use"
423-
);
429+
);
430+
}
431+
if (index >= 0 && index < _enumsByIndex.length) {
432+
return _enumsByIndex[index];
433+
}
434+
} catch (NumberFormatException e) {
435+
// fine, ignore, was not an integer
424436
}
425-
if (index >= 0 && index < _enumsByIndex.length) {
426-
return _enumsByIndex[index];
427-
}
428-
} catch (NumberFormatException e) {
429-
// fine, ignore, was not an integer
430437
}
431438
}
432439
}

src/test/java/com/fasterxml/jackson/failing/EnumDefaultRead4403Test.java renamed to src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumDefaultRead4403Test.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.fasterxml.jackson.failing;
1+
package com.fasterxml.jackson.databind.deser.enums;
22

33
import org.junit.jupiter.api.Test;
44

0 commit comments

Comments
 (0)