Skip to content

Commit 5dfd86f

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

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

Diff for: release-notes/CREDITS-2.x

+4
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)

Diff for: release-notes/VERSION-2.x

+10-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.10.0-final (not yet released)
8+
9+
#2309: READ_ENUMS_USING_TO_STRING doesn't support null values
10+
(reported, fix suggested by Ben A)
11+
#2442: `ArrayNode.addAll()` adds raw `null` values which cause NPE on `deepCopy()`
12+
and `toString()`
13+
(reported, fix contributed by Hesham M)
14+
#2446: Java 11: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
15+
(reported by David C)
16+
717
2.10.0.pr2 (31-Aug-2019)
818
919
#2237: Add "required" methods in `JsonNode`: `required(String | int)`,
@@ -34,11 +44,6 @@ Project: jackson-databind
3444
#2430: Change `ObjectMapper.valueToTree()` to convert `null` to `NullNode`
3545
#2433: Improve `NullNode.equals()`
3646
(suggested by David B)
37-
#2442: `ArrayNode.addAll()` adds raw `null` values which cause NPE on `deepCopy()`
38-
and `toString()`
39-
(reported, fix contributed by Hesham M)
40-
#2446: Java 11: Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added
41-
(reported by David C)
4247

4348
2.10.0.pr1 (19-Jul-2019)
4449

Diff for: src/main/java/com/fasterxml/jackson/databind/util/CompactStringObjectMap.java

+5
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ public static <T> CompactStringObjectMap construct(Map<String,T> all)
5353
for (Map.Entry<String,T> entry : all.entrySet()) {
5454
String key = entry.getKey();
5555

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

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

+43-12
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
import com.fasterxml.jackson.annotation.*;
88
import com.fasterxml.jackson.core.*;
99
import com.fasterxml.jackson.core.type.TypeReference;
10+
1011
import com.fasterxml.jackson.databind.*;
1112
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
1213
import com.fasterxml.jackson.databind.deser.std.FromStringDeserializer;
1314
import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
1415
import com.fasterxml.jackson.databind.exc.InvalidFormatException;
1516
import com.fasterxml.jackson.databind.exc.MismatchedInputException;
17+
import com.fasterxml.jackson.databind.json.JsonMapper;
1618
import com.fasterxml.jackson.databind.module.SimpleModule;
1719

1820
@SuppressWarnings("serial")
@@ -183,6 +185,25 @@ public static ObjectMapper setupObjectMapper(ObjectMapper mapper) {
183185
}
184186
}
185187

188+
// for [databind#2309]
189+
static enum Enum2309 {
190+
NON_NULL("NON_NULL"),
191+
NULL(null),
192+
OTHER("OTHER")
193+
;
194+
195+
private String value;
196+
197+
private Enum2309(String value) {
198+
this.value = value;
199+
}
200+
201+
@Override
202+
public String toString() {
203+
return value;
204+
}
205+
}
206+
186207
/*
187208
/**********************************************************
188209
/* Test methods
@@ -290,11 +311,11 @@ public void testNumbersToEnums() throws Exception
290311

291312
public void testEnumsWithIndex() throws Exception
292313
{
293-
ObjectMapper m = new ObjectMapper();
294-
m.enable(SerializationFeature.WRITE_ENUMS_USING_INDEX);
295-
String json = m.writeValueAsString(TestEnum.RULES);
314+
String json = MAPPER.writer()
315+
.with(SerializationFeature.WRITE_ENUMS_USING_INDEX)
316+
.writeValueAsString(TestEnum.RULES);
296317
assertEquals(String.valueOf(TestEnum.RULES.ordinal()), json);
297-
TestEnum result = m.readValue(json, TestEnum.class);
318+
TestEnum result = MAPPER.readValue(json, TestEnum.class);
298319
assertSame(TestEnum.RULES, result);
299320
}
300321

@@ -391,10 +412,10 @@ public void testGenericEnumDeserialization() throws Exception
391412

392413
// [databind#381]
393414
public void testUnwrappedEnum() throws Exception {
394-
final ObjectMapper mapper = newJsonMapper();
395-
mapper.enable(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS);
396-
397-
assertEquals(TestEnum.JACKSON, mapper.readValue("[" + quote("JACKSON") + "]", TestEnum.class));
415+
assertEquals(TestEnum.JACKSON,
416+
MAPPER.readerFor(TestEnum.class)
417+
.with(DeserializationFeature.UNWRAP_SINGLE_VALUE_ARRAYS)
418+
.readValue("[" + quote("JACKSON") + "]"));
398419
}
399420

400421
public void testUnwrappedEnumException() throws Exception {
@@ -422,11 +443,12 @@ public void testIndexAsString() throws Exception
422443
assertSame(TestEnum.values()[1], en);
423444

424445
// [databind#1690]: unless prevented
425-
final ObjectMapper mapper = jsonMapperBuilder()
426-
.disable(MapperFeature.ALLOW_COERCION_OF_SCALARS)
427-
.build();
428446
try {
429-
en = mapper.readValue(quote("1"), TestEnum.class);
447+
en = JsonMapper.builder()
448+
.configure(MapperFeature.ALLOW_COERCION_OF_SCALARS, false)
449+
.build()
450+
.readerFor(TestEnum.class)
451+
.readValue(quote("1"));
430452
fail("Should not pass");
431453
} catch (MismatchedInputException e) {
432454
verifyException(e, "Cannot deserialize value of type");
@@ -528,4 +550,13 @@ public void testExceptionFromCustomEnumKeyDeserializer() throws Exception {
528550
assertTrue(e.getMessage().contains("Undefined AnEnum"));
529551
}
530552
}
553+
554+
// [databind#2309]
555+
public void testEnumToStringNull2309() throws Exception
556+
{
557+
Enum2309 value = MAPPER.readerFor(Enum2309.class)
558+
.with(DeserializationFeature.READ_ENUMS_USING_TO_STRING)
559+
.readValue(quote("NON_NULL"));
560+
assertEquals(Enum2309.NON_NULL, value);
561+
}
531562
}

0 commit comments

Comments
 (0)