Skip to content

Commit

Permalink
Fix #2725
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 19, 2020
1 parent 1b5308e commit 47f0080
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1702,7 +1702,8 @@ private KeyDeserializer _createEnumKeyDeserializer(DeserializationContext ctxt,
}
}
EnumResolver enumRes = constructEnumResolver(enumClass, config, beanDesc.findJsonValueAccessor());
// May have @JsonCreator for static factory method:

// May have @JsonCreator for static factory method
for (AnnotatedMethod factory : beanDesc.getFactoryMethods()) {
if (_hasCreatorAnnotation(ctxt, factory)) {
int argCount = factory.getParameterCount();
Expand All @@ -1712,7 +1713,11 @@ private KeyDeserializer _createEnumKeyDeserializer(DeserializationContext ctxt,
if (returnType.isAssignableFrom(enumClass)) {
// note: mostly copied from 'EnumDeserializer.deserializerForCreator(...)'
if (factory.getRawParameterType(0) != String.class) {
throw new IllegalArgumentException("Parameter #0 type for factory method ("+factory+") not suitable, must be java.lang.String");
// [databind#2725]: Should not error out because (1) there may be good creator
// method and (2) this method may be valid for "regular" enum value deserialization
// (leaving aside potential for multiple conflicting creators)
// throw new IllegalArgumentException("Parameter #0 type for factory method ("+factory+") not suitable, must be java.lang.String");
continue;
}
if (config.canOverrideAccessModifiers()) {
ClassUtil.checkAndFixAccess(factory.getMember(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.fasterxml.jackson.databind.deser.jdk;

import java.util.Collections;
import java.util.Map;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import com.fasterxml.jackson.core.Base64Variants;
import com.fasterxml.jackson.core.type.TypeReference;

import com.fasterxml.jackson.databind.*;

import org.junit.Assert;
Expand Down Expand Up @@ -37,6 +40,31 @@ public String toString() {
}
}

// [databind#2725]
enum TestEnum2725 {
FOO(1);

private final int i;

TestEnum2725(final int i) {
this.i = i;
}

@JsonValue
public int getI() {
return i;
}

@JsonCreator
public static TestEnum2725 getByIntegerId(final Integer id) {
return id == FOO.i ? FOO : null;
}

@JsonCreator
public static TestEnum2725 getByStringId(final String id) {
return Integer.parseInt(id) == FOO.i ? FOO : null;
}
}
/*
/**********************************************************
/* Test methods, wrapper keys
Expand Down Expand Up @@ -138,4 +166,16 @@ public void testByteArrayMapKeyDeserialization() throws Exception
byte[] key = entry.getKey();
Assert.assertArrayEquals(binary, key);
}

// [databind#2725]
public void testEnumWithCreatorMapKeyDeserialization() throws Exception
{
final Map<TestEnum2725, String> input = Collections.singletonMap(TestEnum2725.FOO, "Hello");
final String json = MAPPER.writeValueAsString(input);
final Map<TestEnum2725, String> output = MAPPER.readValue(json,
new TypeReference<Map<TestEnum2725, String>>() { });

assertNotNull(output);
assertEquals(1, output.size());
}
}

This file was deleted.

0 comments on commit 47f0080

Please sign in to comment.