Skip to content

Commit

Permalink
Fix #1441
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Nov 3, 2016
1 parent 31ceee7 commit cd5430a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 2 deletions.
4 changes: 4 additions & 0 deletions release-notes/CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,10 @@ Kevin Donnelly (kpdonn@github)
* Reported #1432: Off by 1 bug in PropertyValueBuffer
(2.8.5)

Nathanial Ofiesh (ofiesh@github)
* Reported #1441: Failure with custom Enum key deserializer, polymorphic types
(2.8.5)

Connor Kuhn (ckuhn@github)
* Contributed #1341: FAIL_ON_MISSING_EXTERNAL_TYPE_ID_PROPERTY
(2.9.0)
2 changes: 2 additions & 0 deletions release-notes/VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Project: jackson-databind
#1432: Off by 1 bug in PropertyValueBuffer
(reported by Kevin D)
#1439: NPE when using with filter id, serializing `java.util.Map` types
#1441: Failure with custom Enum key deserializer, polymorphic types
(reported by Nathanial O)
- Improvements to #1411 fix to ensure consistent `null` key handling

2.8.4 (14-Oct-2016)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@
import java.net.URL;
import java.util.*;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.io.NumberInput;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JacksonStdImpl;
import com.fasterxml.jackson.databind.introspect.AnnotatedMethod;
import com.fasterxml.jackson.databind.util.ClassUtil;
import com.fasterxml.jackson.databind.util.EnumResolver;
import com.fasterxml.jackson.databind.util.TokenBuffer;

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

@SuppressWarnings("resource")
@Override
public final Object deserializeKey(String key, DeserializationContext ctxt)
throws IOException, JsonProcessingException
throws IOException
{
if (key == null) { // is this even legal call?
return null;
}
TokenBuffer tb = new TokenBuffer(ctxt.getParser(), ctxt);
tb.writeString(key);
try {
// Ugh... should not have to give parser which may or may not be correct one...
Object result = _delegate.deserialize(ctxt.getParser(), ctxt);
JsonParser p = tb.asParser();
p.nextToken();
Object result = _delegate.deserialize(p, ctxt);
if (result != null) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;

Expand Down Expand Up @@ -149,6 +150,17 @@ public void setupModule(SetupContext context) {
}
}

// for [databind#1441]

enum SuperTypeEnum {
FOO;
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "@type", defaultImpl = SuperType.class)
static class SuperType {
public Map<SuperTypeEnum, String> someMap;
}

/*
/**********************************************************
/* Test methods
Expand Down Expand Up @@ -190,4 +202,25 @@ public void withTree749() throws Exception
String firstFieldName = inner.fieldNames().next();
assertEquals("green", firstFieldName);
}

// [databind#1441]
public void testCustomEnumKeySerializerWithPolymorphic() throws IOException
{
SimpleModule simpleModule = new SimpleModule();
simpleModule.addDeserializer(SuperTypeEnum.class, new JsonDeserializer<SuperTypeEnum>() {
@Override
public SuperTypeEnum deserialize(JsonParser p, DeserializationContext deserializationContext)
throws IOException
{
return SuperTypeEnum.valueOf(p.getText());
}
});
ObjectMapper mapper = new ObjectMapper()
.registerModule(simpleModule);

SuperType superType = mapper.readValue("{\"someMap\": {\"FOO\": \"bar\"}}",
SuperType.class);
assertEquals("Deserialized someMap.FOO should equal bar", "bar",
superType.someMap.get(SuperTypeEnum.FOO));
}
}

0 comments on commit cd5430a

Please sign in to comment.