Fix #684
diff --git a/release-notes/VERSION b/release-notes/VERSION
index f931bba..05dd62e 100644
--- a/release-notes/VERSION
+++ b/release-notes/VERSION
@@ -15,6 +15,8 @@
(reported by Zoltan F)
#682: Class<?>-valued Map keys not serialized properly
(reported by Ludevik@github)
+#684: FAIL_ON_NUMBERS_FOR_ENUMS does not fail when integer value is quoted
+ (reported by kllp@github)
- Add a work-around in `ISO8601DateFormat` to allow omission of ':' from timezone
2.5.0 (01-Jan-2015)
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 14881ba..89d549b 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
@@ -76,9 +76,7 @@
// But let's consider int acceptable as well (if within ordinal range)
if (curr == JsonToken.VALUE_NUMBER_INT) {
// ... unless told not to do that. :-) (as per [JACKSON-412]
- if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
- throw ctxt.mappingException("Not allowed to deserialize Enum value out of JSON number (disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow)");
- }
+ _checkFailOnNumber(ctxt);
int index = jp.getIntValue();
Enum<?> result = _resolver.getEnum(index);
@@ -105,6 +103,7 @@
if (c >= '0' && c <= '9') {
try {
int ix = Integer.parseInt(name);
+ _checkFailOnNumber(ctxt);
Enum<?> result = _resolver.getEnum(ix);
if (result != null) {
return result;
@@ -121,7 +120,7 @@
return null;
}
- private final Enum<?> _deserializeOther(JsonParser jp, DeserializationContext ctxt) throws IOException
+ protected Enum<?> _deserializeOther(JsonParser jp, DeserializationContext ctxt) throws IOException
{
JsonToken curr = jp.getCurrentToken();
// Issue#381
@@ -137,7 +136,14 @@
}
throw ctxt.mappingException(_resolver.getEnumClass());
}
-
+
+ protected void _checkFailOnNumber(DeserializationContext ctxt) throws IOException
+ {
+ if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) {
+ throw ctxt.mappingException("Not allowed to deserialize Enum value out of JSON number (disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow)");
+ }
+ }
+
/*
/**********************************************************
/* Additional helper classes
diff --git a/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java b/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java
index c0b9097..e553ec5 100644
--- a/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java
+++ b/src/test/java/com/fasterxml/jackson/databind/deser/TestEnumDeserialization.java
@@ -245,10 +245,18 @@
assertSame(TestEnum.RULES, value);
// but can also be changed to errors:
- ObjectMapper m = new ObjectMapper();
- m.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
+ ObjectReader r = MAPPER.reader(TestEnum.class)
+ .with(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS);
try {
- value = m.readValue("1", TestEnum.class);
+ value = r.readValue("1");
+ fail("Expected an error");
+ } catch (JsonMappingException e) {
+ verifyException(e, "Not allowed to deserialize Enum value out of JSON number");
+ }
+
+ // and [databind#684]
+ try {
+ value = r.readValue(quote("1"));
fail("Expected an error");
} catch (JsonMappingException e) {
verifyException(e, "Not allowed to deserialize Enum value out of JSON number");