Skip to content

Commit 7e553d8

Browse files
committed
Merge branch '2.12' into 2.13
2 parents 4539a55 + 68a9847 commit 7e553d8

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Project: jackson-databind
5555
(reported by JoeWoo; fix provided by drekbour@github)
5656
#3146: Merge findInjectableValues() results in AnnotationIntrospectorPair
5757
(contributed by Joe B)
58+
#3171: READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE doesn't work with empty strings
59+
(reported by unintended@github)
5860

5961
2.12.3 (12-Apr-2021)
6062

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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,27 @@ public void testWithFailOnNumbersAndReadUnknownAsDefault()
207207
_verifyOkDeserialization(r, "2", CustomEnumWithDefault.class, CustomEnumWithDefault.ZERO);
208208
}
209209

210+
// [databind#3171]: Ensure "" gets returned as Default Value, not coerced
211+
public void testEmptyStringAsDefault() throws Exception
212+
{
213+
ObjectReader r = MAPPER.readerFor(SimpleEnumWithDefault.class)
214+
.with(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_USING_DEFAULT_VALUE);
215+
assertEquals(SimpleEnumWithDefault.ONE,
216+
r.readValue(quote("ONE")));
217+
assertEquals(SimpleEnumWithDefault.ZERO,
218+
r.readValue(quote("Zero")));
219+
assertEquals(SimpleEnumWithDefault.ZERO,
220+
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(" ")));
229+
}
230+
210231
private <T> void _verifyOkDeserialization(ObjectReader reader, String fromValue,
211232
Class<T> toValueType, T expValue)
212233
throws IOException

0 commit comments

Comments
 (0)