Skip to content

Commit 68a9847

Browse files
committed
Fix #3171
1 parent 4d97132 commit 68a9847

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Project: jackson-databind
1010
(reported by JoeWoo; fix provided by drekbour@github)
1111
#3146: Merge findInjectableValues() results in AnnotationIntrospectorPair
1212
(contributed by Joe B)
13+
#3171: READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE doesn't work with empty strings
14+
(reported by unintended@github)
1315
1416
2.12.3 (12-Apr-2021)
1517

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,16 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext
273273
{
274274
String name = nameOrig.trim();
275275
if (name.isEmpty()) { // empty or blank
276+
// 07-Jun-2021, tatu: [databind#3171] Need to consider Default value first
277+
// (alas there's bit of duplication here)
278+
if ((_enumDefaultValue != null)
279+
&& ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)) {
280+
return _enumDefaultValue;
281+
}
282+
if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
283+
return null;
284+
}
285+
276286
CoercionAction act;
277287
if (nameOrig.isEmpty()) {
278288
act = _findCoercionFromEmptyString(ctxt);
@@ -323,11 +333,11 @@ private final Object _deserializeAltString(JsonParser p, DeserializationContext
323333
&& ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE)) {
324334
return _enumDefaultValue;
325335
}
326-
if (!ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
327-
return ctxt.handleWeirdStringValue(_enumClass(), name,
328-
"not one of the values accepted for Enum class: %s", lookup.keys());
336+
if (ctxt.isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
337+
return null;
329338
}
330-
return null;
339+
return ctxt.handleWeirdStringValue(_enumClass(), name,
340+
"not one of the values accepted for Enum class: %s", lookup.keys());
331341
}
332342

333343
protected Object _deserializeOther(JsonParser p, DeserializationContext ctxt) throws IOException

src/test/java/com/fasterxml/jackson/databind/deser/jdk/EnumDefaultReadTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,14 @@ public void testEmptyStringAsDefault() throws Exception
218218
r.readValue(quote("Zero")));
219219
assertEquals(SimpleEnumWithDefault.ZERO,
220220
r.readValue(quote("")));
221+
assertEquals(SimpleEnumWithDefault.ZERO,
222+
r.readValue(quote(" ")));
223+
224+
// Also check with `null` coercion as well
225+
ObjectReader r2 = MAPPER.readerFor(SimpleEnumWithDefault.class)
226+
.with(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL);
227+
assertNull(r2.readValue(quote("")));
228+
assertNull(r2.readValue(quote(" ")));
221229
}
222230

223231
private <T> void _verifyOkDeserialization(ObjectReader reader, String fromValue,

0 commit comments

Comments
 (0)