Skip to content

Commit ae3f01b

Browse files
committed
Fix #684
1 parent 8d0d6e4 commit ae3f01b

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

release-notes/VERSION

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ Project: jackson-databind
1515
(reported by Zoltan F)
1616
#682: Class<?>-valued Map keys not serialized properly
1717
(reported by Ludevik@github)
18+
#684: FAIL_ON_NUMBERS_FOR_ENUMS does not fail when integer value is quoted
19+
(reported by kllp@github)
1820
- Add a work-around in `ISO8601DateFormat` to allow omission of ':' from timezone
1921

2022
2.5.0 (01-Jan-2015)

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ public Enum<?> deserialize(JsonParser jp, DeserializationContext ctxt) throws IO
7676
// But let's consider int acceptable as well (if within ordinal range)
7777
if (curr == JsonToken.VALUE_NUMBER_INT) {
7878
// ... unless told not to do that. :-) (as per [JACKSON-412]
79-
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
80-
throw ctxt.mappingException("Not allowed to deserialize Enum value out of JSON number (disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow)");
81-
}
79+
_checkFailOnNumber(ctxt);
8280

8381
int index = jp.getIntValue();
8482
Enum<?> result = _resolver.getEnum(index);
@@ -105,6 +103,7 @@ private final Enum<?> _deserializeAltString(JsonParser jp, DeserializationContex
105103
if (c >= '0' && c <= '9') {
106104
try {
107105
int ix = Integer.parseInt(name);
106+
_checkFailOnNumber(ctxt);
108107
Enum<?> result = _resolver.getEnum(ix);
109108
if (result != null) {
110109
return result;
@@ -121,7 +120,7 @@ private final Enum<?> _deserializeAltString(JsonParser jp, DeserializationContex
121120
return null;
122121
}
123122

124-
private final Enum<?> _deserializeOther(JsonParser jp, DeserializationContext ctxt) throws IOException
123+
protected Enum<?> _deserializeOther(JsonParser jp, DeserializationContext ctxt) throws IOException
125124
{
126125
JsonToken curr = jp.getCurrentToken();
127126
// Issue#381
@@ -137,7 +136,14 @@ private final Enum<?> _deserializeOther(JsonParser jp, DeserializationContext ct
137136
}
138137
throw ctxt.mappingException(_resolver.getEnumClass());
139138
}
140-
139+
140+
protected void _checkFailOnNumber(DeserializationContext ctxt) throws IOException
141+
{
142+
if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
143+
throw ctxt.mappingException("Not allowed to deserialize Enum value out of JSON number (disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow)");
144+
}
145+
}
146+
141147
/*
142148
/**********************************************************
143149
/* Additional helper classes

src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,18 @@ public void testNumbersToEnums() throws Exception
245245
assertSame(TestEnum.RULES, value);
246246

247247
// but can also be changed to errors:
248-
ObjectMapper m = new ObjectMapper();
249-
m.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
248+
ObjectReader r = MAPPER.reader(TestEnum.class)
249+
.with(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS);
250+
try {
251+
value = r.readValue("1");
252+
fail("Expected an error");
253+
} catch (JsonMappingException e) {
254+
verifyException(e, "Not allowed to deserialize Enum value out of JSON number");
255+
}
256+
257+
// and [databind#684]
250258
try {
251-
value = m.readValue("1", TestEnum.class);
259+
value = r.readValue(quote("1"));
252260
fail("Expected an error");
253261
} catch (JsonMappingException e) {
254262
verifyException(e, "Not allowed to deserialize Enum value out of JSON number");

0 commit comments

Comments
 (0)