Skip to content

Commit f645a74

Browse files
committed
Add a test to verify weird-key handler also works
1 parent f2c40cb commit f645a74

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public Object deserializeKey(String key, DeserializationContext ctxt)
125125
return result;
126126
}
127127
} catch (Exception re) {
128-
return ctxt.handleWeirdKey(_keyClass, key, "not a valid representation: %s", re.getMessage());
128+
return ctxt.handleWeirdKey(_keyClass, key, "not a valid representation, problem: %s", re.getMessage());
129129
}
130130
if (_keyClass.isEnum() && ctxt.getConfig().isEnabled(DeserializationFeature.READ_UNKNOWN_ENUM_VALUES_AS_NULL)) {
131131
return null;

src/test/java/com/fasterxml/jackson/databind/filter/ProblemHandlerTest.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
11
package com.fasterxml.jackson.databind.filter;
22

33
import java.io.IOException;
4+
import java.util.Map;
45

56
import com.fasterxml.jackson.annotation.JsonTypeInfo;
67
import com.fasterxml.jackson.databind.*;
78
import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler;
89

910
public class ProblemHandlerTest extends BaseMapTest
1011
{
12+
static class WeirdKeyHandler
13+
extends DeserializationProblemHandler
14+
{
15+
protected final Object key;
16+
17+
public WeirdKeyHandler(Object key0) {
18+
key = key0;
19+
}
20+
21+
@Override
22+
public Object handleWeirdKey(DeserializationContext ctxt,
23+
Class<?> rawKeyType, String keyValue,
24+
String failureMsg)
25+
throws IOException
26+
{
27+
return key;
28+
}
29+
}
30+
31+
static class IntKeyMapWrapper {
32+
public Map<Integer,String> stuff;
33+
}
34+
1135
static class TypeIdHandler
1236
extends DeserializationProblemHandler
1337
{
@@ -37,6 +61,18 @@ static class BaseWrapper {
3761
/**********************************************************
3862
*/
3963

64+
public void testWeirdKeyHandling() throws Exception
65+
{
66+
ObjectMapper mapper = new ObjectMapper()
67+
.addHandler(new WeirdKeyHandler(7));
68+
IntKeyMapWrapper w = mapper.readValue("{\"stuff\":{\"foo\":\"abc\"}}",
69+
IntKeyMapWrapper.class);
70+
Map<Integer,String> map = w.stuff;
71+
assertEquals(1, map.size());
72+
assertEquals("abc", map.values().iterator().next());
73+
assertEquals(Integer.valueOf(7), map.keySet().iterator().next());
74+
}
75+
4076
public void testInvalidTypeId() throws Exception
4177
{
4278
ObjectMapper mapper = new ObjectMapper()

0 commit comments

Comments
 (0)