diff --git a/src/test/java/tools/jackson/failing/CustomObjectKeyDeserializer4680Test.java b/src/test/java/tools/jackson/failing/CustomObjectKeyDeserializer4680Test.java new file mode 100644 index 0000000000..9454c964cf --- /dev/null +++ b/src/test/java/tools/jackson/failing/CustomObjectKeyDeserializer4680Test.java @@ -0,0 +1,71 @@ +package tools.jackson.failing; + +import java.util.Map; + +import tools.jackson.core.type.TypeReference; +import tools.jackson.databind.DeserializationContext; +import tools.jackson.databind.KeyDeserializer; +import tools.jackson.databind.ObjectMapper; +import tools.jackson.databind.json.JsonMapper; +import tools.jackson.databind.module.SimpleModule; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +// [databind#4680] Custom key deserialiser registered for `Object.class` is ignored on nested JSON +public class CustomObjectKeyDeserializer4680Test +{ + + @SuppressWarnings("unchecked") + @Test + void testCustomKeyDeserializer() + throws Exception + { + // GIVEN + String json = + "{\n" + + " \"name*\": \"Erik\",\n" + + " \"address*\": {\n" + + " \"city*\": {\n" + + " \"id*\": 1,\n" + + " \"name*\": \"Berlin\"\n" + + " },\n" + + " \"street*\": \"Elvirastr\"\n" + + " }\n" + + " }"; + + SimpleModule keySanitizationModule = new SimpleModule("key-sanitization"); + keySanitizationModule.addKeyDeserializer(String.class, new KeyDeserializer() { + @Override + public String deserializeKey(String key, DeserializationContext ctxt) { + return key.replace("*", "_"); + } + }); + + keySanitizationModule.addKeyDeserializer(Object.class, new KeyDeserializer() { + @Override + public Object deserializeKey(String key, DeserializationContext ctxt) { + return key.replace("*", "_"); + } + }); + + ObjectMapper mapper = JsonMapper.builder().addModule(keySanitizationModule).build(); + + // WHEN + Map result = mapper.readValue(json, new TypeReference>() { + }); + + // THEN + // depth 1 works as expected + Assertions.assertEquals("Erik", result.get("name_")); + + // before fix, depth 2 does NOT work as expected + Map addressMap = (Map) result.get("address_"); + // before fix, null?? Fails here + Assertions.assertEquals("Elvirastr", addressMap.get("street_")); + Map cityMap = (Map) addressMap.get("city_"); + Assertions.assertEquals(1, cityMap.get("id_")); + Assertions.assertEquals("Berlin", cityMap.get("name_")); + } + +} \ No newline at end of file