1+ package com .fasterxml .jackson .failing ;
2+
3+ import java .util .Map ;
4+
5+ import com .fasterxml .jackson .core .type .TypeReference ;
6+ import com .fasterxml .jackson .databind .DeserializationContext ;
7+ import com .fasterxml .jackson .databind .KeyDeserializer ;
8+ import com .fasterxml .jackson .databind .ObjectMapper ;
9+ import com .fasterxml .jackson .databind .json .JsonMapper ;
10+ import com .fasterxml .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+ @ Test
20+ void testCustomKeyDeserializer ()
21+ throws Exception
22+ {
23+ // GIVEN
24+ String json =
25+ "{\n " +
26+ " \" name*\" : \" Erik\" ,\n " +
27+ " \" address*\" : {\n " +
28+ " \" city*\" : {\n " +
29+ " \" id*\" : 1,\n " +
30+ " \" name*\" : \" Berlin\" \n " +
31+ " },\n " +
32+ " \" street*\" : \" Elvirastr\" \n " +
33+ " }\n " +
34+ " }" ;
35+
36+ SimpleModule keySanitizationModule = new SimpleModule ("key-sanitization" );
37+ keySanitizationModule .addKeyDeserializer (String .class , new KeyDeserializer () {
38+ @ Override
39+ public String deserializeKey (String key , DeserializationContext ctxt ) {
40+ return key .replace ("*" , "_" );
41+ }
42+ });
43+
44+ keySanitizationModule .addKeyDeserializer (Object .class , new KeyDeserializer () {
45+ @ Override
46+ public Object deserializeKey (String key , DeserializationContext ctxt ) {
47+ return key .replace ("*" , "_" );
48+ }
49+ });
50+
51+ ObjectMapper mapper = JsonMapper .builder ().addModule (keySanitizationModule ).build ();
52+
53+ // WHEN
54+ Map <String , Object > result = mapper .readValue (json , new TypeReference <Map <String , Object >>() {
55+ });
56+
57+ // THEN
58+ // depth 1 works as expected
59+ Assertions .assertEquals ("Erik" , result .get ("name_" ));
60+
61+ // before fix, depth 2 does NOT work as expected
62+ Map <String , Object > addressMap = (Map <String , Object >) result .get ("address_" );
63+ // before fix, null?? Fails here
64+ Assertions .assertEquals ("Elvirastr" , addressMap .get ("street_" ));
65+ Map <String , Object > cityMap = (Map <String , Object >) addressMap .get ("city_" );
66+ Assertions .assertEquals (1 , cityMap .get ("id_" ));
67+ Assertions .assertEquals ("Berlin" , cityMap .get ("name_" ));
68+ }
69+
70+ }
0 commit comments