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

Fix #4481: allow override of READ_UNKNOWN_ENUM_VALUES_AS_NULL #4486

Merged
merged 1 commit into from
Apr 19, 2024
Merged
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
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ Project: jackson-databind
(fix by Joo-Hyuk K)
#4450: Empty QName deserialized as `null`
(reported by @winfriedgerlach)
#4481: Unable to override `DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
with `JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL`
(reported by @luozhenyu)

2.17.0 (12-Mar-2024)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,10 @@ protected CompactStringObjectMap _getToStringLookup(DeserializationContext ctxt)

// @since 2.15
protected boolean useNullForUnknownEnum(DeserializationContext ctxt) {
return Boolean.TRUE.equals(_useNullForUnknownEnum)
|| ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
if (_useNullForUnknownEnum != null) {
return _useNullForUnknownEnum;
}
return ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
}

// @since 2.15
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ enum MyEnum2352_3 {
C;
}

// [databind#4481]: override for "unknown as null"
enum Color {
RED, BLUE
}

static class Book4481 {
@JsonFormat(without = JsonFormat.Feature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
public Color color;
}

/*
/**********************************************************
/* Test methods, basic
Expand Down Expand Up @@ -304,4 +314,25 @@ public void testEnumWithNullForUnknownValueEnumSet() throws Exception {
assertEquals(1, pojo.value.size());
assertTrue(pojo.value.contains(MyEnum2352_3.B));
}

/*
/**********************************************************
/* Test methods, other
/**********************************************************
*/

// [databind#4481]
@Test
public void testDefaultFromNullOverride4481() throws Exception
{
try {
Book4481 book = MAPPER.readerFor(Book4481.class)
.with(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)
.readValue("{\"color\":\"WHITE\"}");
fail("Should have failed; got: "+book.color);
} catch (InvalidFormatException e) {
verifyException(e, "Cannot deserialize value of type ");
verifyException(e, "not one of the values accepted for Enum class");
}
}
}