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

Prevent use of quoted number (index) for Enum deserialization via MapperFeature.ALLOW_COERCION_OF_SCALARS #1690

Closed
wants to merge 1 commit into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,24 @@ public enum DeserializationFeature implements ConfigFeature
*
* @since 2.1
*/
EAGER_DESERIALIZER_FETCH(true)
EAGER_DESERIALIZER_FETCH(true),

/**
* Feature that determines whether JSON string mapping are valid
* values to be used to map to ordinal() of matching enumeration value
* for deserializing Java enum values when string itself is a number.
* If set to 'false' string are acceptable as ordinal values when
* and are used to map to
* ordinal() of matching enumeration value; if 'true', strings are
* not allowed to be ordinal mapping and
* a {@link JsonMappingException} will be thrown.
* Latter behavior makes sense if there is concern that accidental
* mapping from string values to enums ordinal might happen (and when enums
* are always serialized as JSON Strings)
*<p>
* Feature is disabled by default.
*/
FAIL_ON_ORDINAL_STRING_MAPPING_FOR_ENUMS(false)

;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,11 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext
try {
int index = Integer.parseInt(name);
if (index >= 0 && index < _enumsByIndex.length) {
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_ORDINAL_STRING_MAPPING_FOR_ENUMS)) {
return ctxt.handleWeirdStringValue(_enumClass(), name,
"not allowed to deserialize Enum value by ordinal out of string: disable DeserializationConfig.DeserializationFeature.FAIL_ON_ORDINAL_STRING_MAPPING_FOR_ENUMS to allow"
);
}
return _enumsByIndex[index];
}
} catch (NumberFormatException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,22 @@ public void testEnumsWithJsonValue() throws Exception
e = MAPPER.readValue(quote("bar"), EnumWithJsonValue.class);
assertSame(EnumWithJsonValue.B, e);

// then, enum as string ordinal
EnumWithJsonValue enumByStringOrdinal = MAPPER.readValue(quote("0"), EnumWithJsonValue.class);
assertSame(EnumWithJsonValue.A, enumByStringOrdinal);
enumByStringOrdinal = MAPPER.readValue(quote("1"), EnumWithJsonValue.class);
assertSame(EnumWithJsonValue.B, enumByStringOrdinal);

// but can also be changed to errors:
ObjectReader r = MAPPER.readerFor(EnumWithJsonValue.class)
.with(DeserializationFeature.FAIL_ON_ORDINAL_STRING_MAPPING_FOR_ENUMS);;
try {
enumByStringOrdinal = r.readValue(quote("0"));
fail("Expected an error");
} catch (JsonMappingException exc) {
verifyException(exc, "Can not deserialize");
verifyException(exc, "not allowed to deserialize Enum value by ordinal out of string");
}
// then in EnumSet
EnumSet<EnumWithJsonValue> set = MAPPER.readValue("[\"bar\"]",
new TypeReference<EnumSet<EnumWithJsonValue>>() { });
Expand Down