Skip to content

Commit 47f0080

Browse files
committed
Fix #2725
1 parent 1b5308e commit 47f0080

File tree

3 files changed

+47
-58
lines changed

3 files changed

+47
-58
lines changed

src/main/java/com/fasterxml/jackson/databind/deser/BasicDeserializerFactory.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,8 @@ private KeyDeserializer _createEnumKeyDeserializer(DeserializationContext ctxt,
17021702
}
17031703
}
17041704
EnumResolver enumRes = constructEnumResolver(enumClass, config, beanDesc.findJsonValueAccessor());
1705-
// May have @JsonCreator for static factory method:
1705+
1706+
// May have @JsonCreator for static factory method
17061707
for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) {
17071708
if (_hasCreatorAnnotation(ctxt, factory)) {
17081709
int argCount = factory.getParameterCount();
@@ -1712,7 +1713,11 @@ private KeyDeserializer _createEnumKeyDeserializer(DeserializationContext ctxt,
17121713
if (returnType.isAssignableFrom(enumClass)) {
17131714
// note: mostly copied from 'EnumDeserializer.deserializerForCreator(...)'
17141715
if (factory.getRawParameterType(0) != String.class) {
1715-
throw new IllegalArgumentException("Parameter #0 type for factory method ("+factory+") not suitable, must be java.lang.String");
1716+
// [databind#2725]: Should not error out because (1) there may be good creator
1717+
// method and (2) this method may be valid for "regular" enum value deserialization
1718+
// (leaving aside potential for multiple conflicting creators)
1719+
// throw new IllegalArgumentException("Parameter #0 type for factory method ("+factory+") not suitable, must be java.lang.String");
1720+
continue;
17161721
}
17171722
if (config.canOverrideAccessModifiers()) {
17181723
ClassUtil.checkAndFixAccess(factory.getMember(),

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

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

3+
import java.util.Collections;
34
import java.util.Map;
45

56
import com.fasterxml.jackson.annotation.JsonCreator;
67
import com.fasterxml.jackson.annotation.JsonValue;
8+
79
import com.fasterxml.jackson.core.Base64Variants;
810
import com.fasterxml.jackson.core.type.TypeReference;
11+
912
import com.fasterxml.jackson.databind.*;
1013

1114
import org.junit.Assert;
@@ -37,6 +40,31 @@ public String toString() {
3740
}
3841
}
3942

43+
// [databind#2725]
44+
enum TestEnum2725 {
45+
FOO(1);
46+
47+
private final int i;
48+
49+
TestEnum2725(final int i) {
50+
this.i = i;
51+
}
52+
53+
@JsonValue
54+
public int getI() {
55+
return i;
56+
}
57+
58+
@JsonCreator
59+
public static TestEnum2725 getByIntegerId(final Integer id) {
60+
return id == FOO.i ? FOO : null;
61+
}
62+
63+
@JsonCreator
64+
public static TestEnum2725 getByStringId(final String id) {
65+
return Integer.parseInt(id) == FOO.i ? FOO : null;
66+
}
67+
}
4068
/*
4169
/**********************************************************
4270
/* Test methods, wrapper keys
@@ -138,4 +166,16 @@ public void testByteArrayMapKeyDeserialization() throws Exception
138166
byte[] key = entry.getKey();
139167
Assert.assertArrayEquals(binary, key);
140168
}
169+
170+
// [databind#2725]
171+
public void testEnumWithCreatorMapKeyDeserialization() throws Exception
172+
{
173+
final Map<TestEnum2725, String> input = Collections.singletonMap(TestEnum2725.FOO, "Hello");
174+
final String json = MAPPER.writeValueAsString(input);
175+
final Map<TestEnum2725, String> output = MAPPER.readValue(json,
176+
new TypeReference<Map<TestEnum2725, String>>() { });
177+
178+
assertNotNull(output);
179+
assertEquals(1, output.size());
180+
}
141181
}

src/test/java/com/fasterxml/jackson/failing/EnumAsMapKey2725Test.java

-56
This file was deleted.

0 commit comments

Comments
 (0)