Skip to content

Commit ccabd8e

Browse files
authored
Fix #4355: don't fail getting serializer for Enum with toString() returning null (#4360)
1 parent 23551ec commit ccabd8e

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

release-notes/VERSION-2.x

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ Project: jackson-databind
1717
#4316: NPE when deserializing `JsonAnySetter` in `Throwable`
1818
(reported by @jpraet)
1919
(fix contributed by Joo-Hyuk K)
20+
#4355: Jackson 2.16 fails attempting to obtain `ObjectWriter` for an `Enum` of which
21+
some value returns null from `toString()`
22+
(reported by @YutaHiguchi-bsn)
2023
2124
2.16.1 (24-Dec-2023)
2225

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public static EnumValues constructFromToString(MapperConfig<?> config, Annotated
129129
if (name == null) {
130130
Enum<?> en = enumConstants[i];
131131
name = en.toString();
132+
// 01-Feb-2024, tatu: [databind#4355] Nulls not great but... let's
133+
// coerce into "" for backwards compatibility
134+
if (name == null) {
135+
name = "";
136+
}
132137
}
133138
if (useLowerCase) {
134139
name = name.toLowerCase();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.fasterxml.jackson.databind.deser.enums;
2+
3+
import com.fasterxml.jackson.databind.*;
4+
5+
public class EnumWithNullToString4355Test extends BaseMapTest
6+
{
7+
// [databind#4355]
8+
enum Enum4355 {
9+
ALPHA("A"),
10+
BETA("B"),
11+
UNDEFINED(null);
12+
13+
private final String s;
14+
15+
Enum4355(String s) {
16+
this.s = s;
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return s;
22+
}
23+
}
24+
25+
private final ObjectMapper MAPPER = newJsonMapper();
26+
27+
// [databind#4355]
28+
public void testWithNullToString() throws Exception
29+
{
30+
assertEquals("\"ALPHA\"", MAPPER.writeValueAsString(Enum4355.ALPHA));
31+
}
32+
}

0 commit comments

Comments
 (0)