Skip to content

Commit

Permalink
Fix #4355: don't fail getting serializer for Enum with toString()
Browse files Browse the repository at this point in the history
… returning null
  • Loading branch information
cowtowncoder committed Feb 2, 2024
1 parent 23551ec commit 38087ef
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Project: jackson-databind
#4316: NPE when deserializing `JsonAnySetter` in `Throwable`
(reported by @jpraet)
(fix contributed by Joo-Hyuk K)
#4355: Jackson 2.16 fails attempting to obtain `ObjectWriter` for an `Enum` of which
some value returns null from `toString()`
(reported by @YutaHiguchi-bsn)
2.16.1 (24-Dec-2023)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ public static EnumValues constructFromToString(MapperConfig<?> config, Annotated
if (name == null) {
Enum<?> en = enumConstants[i];
name = en.toString();
// 01-Feb-2024, tatu: [databind#4355] Nulls not great but... let's
// coerce into "" for backwards compatibility
if (name == null) {
name = "";
}
}
if (useLowerCase) {
name = name.toLowerCase();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.fasterxml.jackson.databind.deser.enums;

import com.fasterxml.jackson.databind.*;

public class EnumWithNullToString4355Test extends BaseMapTest
{
// [databind#4355]
enum Enum4355 {
ALPHA("A"),
BETA("B"),
UNDEFINED(null);

private final String s;

Enum4355(String s) {
this.s = s;
}

@Override
public String toString() {
return s;
}
}

private final ObjectMapper MAPPER = newJsonMapper();

// [databind#4355]
public void testWithNullToString() throws Exception
{
assertEquals("\"ALPHA\"", MAPPER.writeValueAsString(Enum4355.ALPHA));
}
}

0 comments on commit 38087ef

Please sign in to comment.