Skip to content

Commit 70bd6dd

Browse files
committed
Merge branch '2.18'
2 parents f84193d + 5e0d5f6 commit 70bd6dd

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package tools.jackson.failing;
2+
3+
import java.util.Map;
4+
5+
import tools.jackson.core.type.TypeReference;
6+
import tools.jackson.databind.DeserializationContext;
7+
import tools.jackson.databind.KeyDeserializer;
8+
import tools.jackson.databind.ObjectMapper;
9+
import tools.jackson.databind.json.JsonMapper;
10+
import tools.jackson.databind.module.SimpleModule;
11+
12+
import org.junit.jupiter.api.Assertions;
13+
import org.junit.jupiter.api.Test;
14+
15+
// [databind#4680] Custom key deserialiser registered for `Object.class` is ignored on nested JSON
16+
public class CustomObjectKeyDeserializer4680Test
17+
{
18+
19+
@SuppressWarnings("unchecked")
20+
@Test
21+
void testCustomKeyDeserializer()
22+
throws Exception
23+
{
24+
// GIVEN
25+
String json =
26+
"{\n" +
27+
" \"name*\": \"Erik\",\n" +
28+
" \"address*\": {\n" +
29+
" \"city*\": {\n" +
30+
" \"id*\": 1,\n" +
31+
" \"name*\": \"Berlin\"\n" +
32+
" },\n" +
33+
" \"street*\": \"Elvirastr\"\n" +
34+
" }\n" +
35+
" }";
36+
37+
SimpleModule keySanitizationModule = new SimpleModule("key-sanitization");
38+
keySanitizationModule.addKeyDeserializer(String.class, new KeyDeserializer() {
39+
@Override
40+
public String deserializeKey(String key, DeserializationContext ctxt) {
41+
return key.replace("*", "_");
42+
}
43+
});
44+
45+
keySanitizationModule.addKeyDeserializer(Object.class, new KeyDeserializer() {
46+
@Override
47+
public Object deserializeKey(String key, DeserializationContext ctxt) {
48+
return key.replace("*", "_");
49+
}
50+
});
51+
52+
ObjectMapper mapper = JsonMapper.builder().addModule(keySanitizationModule).build();
53+
54+
// WHEN
55+
Map<String, Object> result = mapper.readValue(json, new TypeReference<Map<String, Object>>() {
56+
});
57+
58+
// THEN
59+
// depth 1 works as expected
60+
Assertions.assertEquals("Erik", result.get("name_"));
61+
62+
// before fix, depth 2 does NOT work as expected
63+
Map<String, Object> addressMap = (Map<String, Object>) result.get("address_");
64+
// before fix, null?? Fails here
65+
Assertions.assertEquals("Elvirastr", addressMap.get("street_"));
66+
Map<String, Object> cityMap = (Map<String, Object>) addressMap.get("city_");
67+
Assertions.assertEquals(1, cityMap.get("id_"));
68+
Assertions.assertEquals("Berlin", cityMap.get("name_"));
69+
}
70+
71+
}

0 commit comments

Comments
 (0)