Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4403: prevent use of zero-prefixed String as Enum index on deserialization #4420

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.fasterxml.jackson.failing;
package com.fasterxml.jackson.databind.deser.enums;

import org.junit.jupiter.api.Test;

Expand Down