Skip to content

Commit 02a803a

Browse files
authored
Merge pull request #1332 from fizmax/index-out-of-bound-enum-index-deser
Fixed ArrayIndexOutOfBoundException for enum by index deser
2 parents 6adce38 + 7f6dcbc commit 02a803a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOEx
136136
"not allowed to deserialize Enum value out of number: disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow"
137137
);
138138
}
139-
if (index >= 0 && index <= _enumsByIndex.length) {
139+
if (index >= 0 && index < _enumsByIndex.length) {
140140
return _enumsByIndex[index];
141141
}
142142
if ((_enumDefaultValue != null)

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

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,35 @@ public void testEnumWithDefaultAnnotation() throws Exception {
458458
assertSame(EnumWithDefaultAnno.OTHER, myEnum);
459459
}
460460

461-
public void testEnumWithDefaultAnnotationUsingIndexes() throws Exception {
461+
public void testEnumWithDefaultAnnotationUsingIndexInBound1() throws Exception {
462462
final ObjectMapper mapper = new ObjectMapper();
463463
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
464464

465-
EnumWithDefaultAnno myEnum = mapper.readValue("9", EnumWithDefaultAnno.class);
465+
EnumWithDefaultAnno myEnum = mapper.readValue("1", EnumWithDefaultAnno.class);
466+
assertSame(EnumWithDefaultAnno.B, myEnum);
467+
}
468+
469+
public void testEnumWithDefaultAnnotationUsingIndexInBound2() throws Exception {
470+
final ObjectMapper mapper = new ObjectMapper();
471+
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
472+
473+
EnumWithDefaultAnno myEnum = mapper.readValue("2", EnumWithDefaultAnno.class);
474+
assertSame(EnumWithDefaultAnno.OTHER, myEnum);
475+
}
476+
477+
public void testEnumWithDefaultAnnotationUsingIndexSameAsLength() throws Exception {
478+
final ObjectMapper mapper = new ObjectMapper();
479+
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
480+
481+
EnumWithDefaultAnno myEnum = mapper.readValue("3", EnumWithDefaultAnno.class);
482+
assertSame(EnumWithDefaultAnno.OTHER, myEnum);
483+
}
484+
485+
public void testEnumWithDefaultAnnotationUsingIndexOutOfBound() throws Exception {
486+
final ObjectMapper mapper = new ObjectMapper();
487+
mapper.enable(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
488+
489+
EnumWithDefaultAnno myEnum = mapper.readValue("4", EnumWithDefaultAnno.class);
466490
assertSame(EnumWithDefaultAnno.OTHER, myEnum);
467491
}
468492

0 commit comments

Comments
 (0)