Skip to content

Commit d1959f8

Browse files
committed
Fix #2457 (enum subtypes not working as map keys)
1 parent d132efa commit d1959f8

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

release-notes/CREDITS-2.x

+4
Original file line numberDiff line numberDiff line change
@@ -971,3 +971,7 @@ Martín Coll (colltoaction@github)
971971
* Contributed #2467: Accept `JsonTypeInfo.As.WRAPPER_ARRAY` with no second argument to
972972
deserialize as "null value"
973973
(2.10.0)
974+
975+
Andrey Kulikov (ankulikov@github)
976+
* Reported #2457: Extended enum values are not handled as enums when used as Map keys
977+
(2.10.1)

release-notes/VERSION-2.x

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

7+
2.10.1 (not yet released)
8+
9+
#2457: Extended enum values are not handled as enums when used as Map keys
10+
(reported by Andrey K)
11+
712
2.10.0 (26-Sep-2019)
813

914
#18: Make `JsonNode` serializable

src/main/java/com/fasterxml/jackson/databind/JavaType.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,12 @@ public boolean isConcrete() {
288288
public boolean isArrayType() { return false; }
289289

290290
@Override
291-
public final boolean isEnumType() { return _class.isEnum(); }
291+
public final boolean isEnumType() {
292+
// 29-Sep-2019, tatu: `Class.isEnum()` not enough to detect custom subtypes,
293+
// use this instead
294+
// return Enum.class.isAssignableFrom(_class);
295+
return _class.isEnum();
296+
}
292297

293298
@Override
294299
public final boolean isInterface() { return _class.isInterface(); }

src/main/java/com/fasterxml/jackson/databind/ser/std/StdKeySerializers.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,9 @@ public static JsonSerializer<Object> getFallbackKeySerializer(SerializationConfi
9898
if (rawKeyType == Enum.class) {
9999
return new Dynamic();
100100
}
101-
if (rawKeyType.isEnum()) {
101+
// 29-Sep-2019, tatu: [databind#2457] can not use 'rawKeyType.isEnum()`, won't work
102+
// for subtypes.
103+
if (Enum.class.isAssignableFrom(rawKeyType)) {
102104
return EnumKeySerializer.construct(rawKeyType,
103105
EnumValues.constructFromName(config, (Class<Enum<?>>) rawKeyType));
104106
}

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

+34
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.databind.deser.jdk;
22

33
import java.util.EnumMap;
4+
import java.util.LinkedHashMap;
45
import java.util.Map;
56

67
import com.fasterxml.jackson.annotation.*;
@@ -77,6 +78,22 @@ public Pojo1859(EnumMap<Enum1859, String> v) {
7778
}
7879
}
7980

81+
// [databind#2457]
82+
enum MyEnum2457 {
83+
A,
84+
B() {
85+
// just to ensure subclass construction
86+
@Override
87+
public void foo() { }
88+
};
89+
90+
// needed to force subclassing
91+
public void foo() { }
92+
93+
@Override
94+
public String toString() { return name() + " as string"; }
95+
}
96+
8097
/*
8198
/**********************************************************
8299
/* Test methods, basic
@@ -224,4 +241,21 @@ public void testUnknownKeyAsNull() throws Exception
224241
assertEquals(1, value2.size());
225242
assertEquals("value", value2.get(null));
226243
}
244+
245+
// [databind#2457]
246+
public void testCustomEnumAsRootMapKey() throws Exception
247+
{
248+
final ObjectMapper mapper = newJsonMapper();
249+
final Map<MyEnum2457, String> map = new LinkedHashMap<>();
250+
map.put(MyEnum2457.A, "1");
251+
map.put(MyEnum2457.B, "2");
252+
assertEquals(aposToQuotes("{'A':'1','B':'2'}"),
253+
mapper.writeValueAsString(map));
254+
255+
// But should be able to override
256+
assertEquals(aposToQuotes("{'"+MyEnum2457.A.toString()+"':'1','"+MyEnum2457.B.toString()+"':'2'}"),
257+
mapper.writer()
258+
.with(SerializationFeature.WRITE_ENUMS_USING_TO_STRING)
259+
.writeValueAsString(map));
260+
}
227261
}

0 commit comments

Comments
 (0)