Skip to content

Commit 5d75ef3

Browse files
committed
Fix #2309 (NPE for Enum.toString()) as suggested by Ben A
1 parent a1ab13b commit 5d75ef3

File tree

5 files changed

+51
-13
lines changed

5 files changed

+51
-13
lines changed

release-notes/CREDITS-2.x

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,10 @@ Pavel Chervakov (pacher@github)
892892
* Reported #2230: `WRITE_BIGDECIMAL_AS_PLAIN` is ignored if `@JsonFormat` is used
893893
(2.10.0)
894894

895+
Ben Anderson (andersonbd1@github)
896+
* Reported, suggested fix for #2309: READ_ENUMS_USING_TO_STRING doesn't support null values
897+
(2.10.0)
898+
895899
Manuel Hegner (manuel-hegner@github)
896900
* Suggested #2311: Unnecessary MultiView creation for property writers
897901
(2.10.0)

release-notes/VERSION-2.x

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ Project: jackson-databind
88

99
#2237: Add "required" methods in `JsonNode`: `required(String | int)`,
1010
`requiredAt(JsonPointer)`
11+
#2309: READ_ENUMS_USING_TO_STRING doesn't support null values
12+
(reported, fix suggested by Ben A)
1113
#2331: `JsonMappingException` through nested getter with generic wildcard return type
1214
(reported by sunchezz89@github)
1315
#2336: `MapDeserializer` can not merge `Map`s with polymorphic values

src/main/java/com/fasterxml/jackson/databind/util/CompactStringObjectMap.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ public static CompactStringObjectMap construct(Map<String,?> all)
5151
for (Map.Entry<String,?> entry : all.entrySet()) {
5252
String key = entry.getKey();
5353

54+
// 09-Sep-2019, tatu: [databind#2309] skip `null`s if any included
55+
if (key == null) {
56+
continue;
57+
}
58+
5459
int slot = key.hashCode() & mask;
5560
int ix = slot+slot;
5661

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

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,25 @@ public void setupModule(SetupContext context) {
177177
}
178178
}
179179

180+
// for [databind#2309]
181+
static enum Enum2309 {
182+
NON_NULL("NON_NULL"),
183+
NULL(null),
184+
OTHER("OTHER")
185+
;
186+
187+
private String value;
188+
189+
private Enum2309(String value) {
190+
this.value = value;
191+
}
192+
193+
@Override
194+
public String toString() {
195+
return value;
196+
}
197+
}
198+
180199
/*
181200
/**********************************************************
182201
/* Test methods
@@ -285,12 +304,11 @@ public void testNumbersToEnums() throws Exception
285304

286305
public void testEnumsWithIndex() throws Exception
287306
{
288-
ObjectMapper m = jsonMapperBuilder()
289-
.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX)
290-
.build();
291-
String json = m.writeValueAsString(TestEnum.RULES);
307+
String json = MAPPER.writer()
308+
.with(SerializationFeature.WRITE_ENUMS_USING_INDEX)
309+
.writeValueAsString(TestEnum.RULES);
292310
assertEquals(String.valueOf(TestEnum.RULES.ordinal()), json);
293-
TestEnum result = m.readValue(json, TestEnum.class);
311+
TestEnum result = MAPPER.readValue(json, TestEnum.class);
294312
assertSame(TestEnum.RULES, result);
295313
}
296314

@@ -390,10 +408,10 @@ public void testGenericEnumDeserialization() throws Exception
390408

391409
// [databind#381]
392410
public void testUnwrappedEnum() throws Exception {
393-
final ObjectMapper mapper = jsonMapperBuilder()
394-
.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
395-
.build();
396-
assertEquals(TestEnum.JACKSON, mapper.readValue("[" + quote("JACKSON") + "]", TestEnum.class));
411+
assertEquals(TestEnum.JACKSON,
412+
MAPPER.readerFor(TestEnum.class)
413+
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
414+
.readValue("[" + quote("JACKSON") + "]"));
397415
}
398416

399417
public void testUnwrappedEnumException() throws Exception {
@@ -422,11 +440,10 @@ public void testIndexAsString() throws Exception
422440
assertSame(TestEnum.values()[1], en);
423441

424442
// [databind#1690]: unless prevented
425-
final ObjectMapper mapper = jsonMapperBuilder()
426-
.disable(DeserializationFeature.ALLOW_COERCION_OF_SCALARS)
427-
.build();
428443
try {
429-
en = mapper.readValue(quote("1"), TestEnum.class);
444+
en = MAPPER.readerFor(TestEnum.class)
445+
.without(DeserializationFeature.ALLOW_COERCION_OF_SCALARS)
446+
.readValue(quote("1"));
430447
fail("Should not pass");
431448
} catch (MismatchedInputException e) {
432449
verifyException(e, "Cannot deserialize value of type");
@@ -535,4 +552,13 @@ public void testExceptionFromCustomEnumKeyDeserializer() throws Exception {
535552
assertTrue(e.getMessage().contains("Undefined AnEnum"));
536553
}
537554
}
555+
556+
// [databind#2309]
557+
public void testEnumToStringNull2309() throws Exception
558+
{
559+
Enum2309 value = MAPPER.readerFor(Enum2309.class)
560+
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
561+
.readValue(quote("NON_NULL"));
562+
assertEquals(Enum2309.NON_NULL, value);
563+
}
538564
}

src/test/java/com/fasterxml/jackson/databind/ser/TestEnumSerialization.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static enum OK {
108108
OK(String key) { this.key = key; }
109109
}
110110

111+
@SuppressWarnings("rawtypes")
111112
static class LowerCasingEnumSerializer extends StdSerializer<Enum>
112113
{
113114
public LowerCasingEnumSerializer() { super(Enum.class); }

0 commit comments

Comments
 (0)