Skip to content

Commit 648fde5

Browse files
authored
Fix #4481: allow override of READ_UNKNOWN_ENUM_VALUES_AS_NULL (#4486)
1 parent e71e1a2 commit 648fde5

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

release-notes/VERSION-2.x

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ Project: jackson-databind
2525
(fix by Joo-Hyuk K)
2626
#4450: Empty QName deserialized as `null`
2727
(reported by @winfriedgerlach)
28+
#4481: Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
29+
with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
30+
(reported by @luozhenyu)
2831
2932
2.17.0 (12-Mar-2024)
3033

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,10 @@ protected CompactStringObjectMap _getToStringLookup(DeserializationContext ctxt)
486486

487487
// @since 2.15
488488
protected boolean useNullForUnknownEnum(DeserializationContext ctxt) {
489-
return Boolean.TRUE.equals(_useNullForUnknownEnum)
490-
|| ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
489+
if (_useNullForUnknownEnum != null) {
490+
return _useNullForUnknownEnum;
491+
}
492+
return ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
491493
}
492494

493495
// @since 2.15

src/test/java/com/fasterxml/jackson/databind/deser/enums/EnumAltIdTest.java

+31
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ enum MyEnum2352_3 {
9393
C;
9494
}
9595

96+
// [databind#4481]: override for "unknown as null"
97+
enum Color {
98+
RED, BLUE
99+
}
100+
101+
static class Book4481 {
102+
@JsonFormat(without = JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
103+
public Color color;
104+
}
105+
96106
/*
97107
/**********************************************************
98108
/* Test methods, basic
@@ -304,4 +314,25 @@ public void testEnumWithNullForUnknownValueEnumSet() throws Exception {
304314
assertEquals(1, pojo.value.size());
305315
assertTrue(pojo.value.contains(MyEnum2352_3.B));
306316
}
317+
318+
/*
319+
/**********************************************************
320+
/* Test methods, other
321+
/**********************************************************
322+
*/
323+
324+
// [databind#4481]
325+
@Test
326+
public void testDefaultFromNullOverride4481() throws Exception
327+
{
328+
try {
329+
Book4481 book = MAPPER.readerFor(Book4481.class)
330+
.with(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
331+
.readValue("{\"color\":\"WHITE\"}");
332+
fail("Should have failed; got: "+book.color);
333+
} catch (InvalidFormatException e) {
334+
verifyException(e, "Cannot deserialize value of type ");
335+
verifyException(e, "not one of the values accepted for Enum class");
336+
}
337+
}
307338
}

0 commit comments

Comments
 (0)