Skip to content

Commit cd5430a

Browse files
committed
Fix #1441
1 parent 31ceee7 commit cd5430a

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

release-notes/CREDITS

+4
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ Kevin Donnelly (kpdonn@github)
562562
* Reported #1432: Off by 1 bug in PropertyValueBuffer
563563
(2.8.5)
564564

565+
Nathanial Ofiesh (ofiesh@github)
566+
* Reported #1441: Failure with custom Enum key deserializer, polymorphic types
567+
(2.8.5)
568+
565569
Connor Kuhn (ckuhn@github)
566570
* Contributed #1341: FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY
567571
(2.9.0)

release-notes/VERSION

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Project: jackson-databind
1212
#1432: Off by 1 bug in PropertyValueBuffer
1313
(reported by Kevin D)
1414
#1439: NPE when using with filter id, serializing `java.util.Map` types
15+
#1441: Failure with custom Enum key deserializer, polymorphic types
16+
(reported by Nathanial O)
1517
- Improvements to #1411 fix to ensure consistent `null` key handling
1618

1719
2.8.4 (14-Oct-2016)

src/main/java/com/fasterxml/jackson/databind/deser/std/StdKeyDeserializer.java

+9-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@
88
import java.net.URL;
99
import java.util.*;
1010

11+
import com.fasterxml.jackson.core.JsonParser;
1112
import com.fasterxml.jackson.core.JsonProcessingException;
1213
import com.fasterxml.jackson.core.io.NumberInput;
1314
import com.fasterxml.jackson.databind.*;
1415
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
1516
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
1617
import com.fasterxml.jackson.databind.util.ClassUtil;
1718
import com.fasterxml.jackson.databind.util.EnumResolver;
19+
import com.fasterxml.jackson.databind.util.TokenBuffer;
1820

1921
/**
2022
* Default {@link KeyDeserializer} implementation used for most {@link java.util.Map}
@@ -303,16 +305,21 @@ protected DelegatingKD(Class<?> cls, JsonDeserializer<?> deser) {
303305
_delegate = deser;
304306
}
305307

308+
@SuppressWarnings("resource")
306309
@Override
307310
public final Object deserializeKey(String key, DeserializationContext ctxt)
308-
throws IOException, JsonProcessingException
311+
throws IOException
309312
{
310313
if (key == null) { // is this even legal call?
311314
return null;
312315
}
316+
TokenBuffer tb = new TokenBuffer(ctxt.getParser(), ctxt);
317+
tb.writeString(key);
313318
try {
314319
// Ugh... should not have to give parser which may or may not be correct one...
315-
Object result = _delegate.deserialize(ctxt.getParser(), ctxt);
320+
JsonParser p = tb.asParser();
321+
p.nextToken();
322+
Object result = _delegate.deserialize(p, ctxt);
316323
if (result != null) {
317324
return result;
318325
}

src/test/java/com/fasterxml/jackson/databind/module/TestCustomEnumKeyDeserializer.java

+33
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import org.junit.Test;
88

9+
import com.fasterxml.jackson.annotation.JsonTypeInfo;
910
import com.fasterxml.jackson.core.*;
1011
import com.fasterxml.jackson.core.type.TypeReference;
1112

@@ -149,6 +150,17 @@ public void setupModule(SetupContext context) {
149150
}
150151
}
151152

153+
// for [databind#1441]
154+
155+
enum SuperTypeEnum {
156+
FOO;
157+
}
158+
159+
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type", defaultImpl = SuperType.class)
160+
static class SuperType {
161+
public Map<SuperTypeEnum, String> someMap;
162+
}
163+
152164
/*
153165
/**********************************************************
154166
/* Test methods
@@ -190,4 +202,25 @@ public void withTree749() throws Exception
190202
String firstFieldName = inner.fieldNames().next();
191203
assertEquals("green", firstFieldName);
192204
}
205+
206+
// [databind#1441]
207+
public void testCustomEnumKeySerializerWithPolymorphic() throws IOException
208+
{
209+
SimpleModule simpleModule = new SimpleModule();
210+
simpleModule.addDeserializer(SuperTypeEnum.class, new JsonDeserializer<SuperTypeEnum>() {
211+
@Override
212+
public SuperTypeEnum deserialize(JsonParser p, DeserializationContext deserializationContext)
213+
throws IOException
214+
{
215+
return SuperTypeEnum.valueOf(p.getText());
216+
}
217+
});
218+
ObjectMapper mapper = new ObjectMapper()
219+
.registerModule(simpleModule);
220+
221+
SuperType superType = mapper.readValue("{\"someMap\": {\"FOO\": \"bar\"}}",
222+
SuperType.class);
223+
assertEquals("Deserialized someMap.FOO should equal bar", "bar",
224+
superType.someMap.get(SuperTypeEnum.FOO));
225+
}
193226
}

0 commit comments

Comments
 (0)