-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create CustomObjectKeyDeserializer4680Test.java (#4685)
- Loading branch information
1 parent
450071b
commit 5e0d5f6
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/test/java/com/fasterxml/jackson/failing/CustomObjectKeyDeserializer4680Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.fasterxml.jackson.failing; | ||
|
||
import java.util.Map; | ||
|
||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.KeyDeserializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.json.JsonMapper; | ||
import com.fasterxml.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 | ||
{ | ||
|
||
@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<String, Object> result = mapper.readValue(json, new TypeReference<Map<String, Object>>() { | ||
}); | ||
|
||
// THEN | ||
// depth 1 works as expected | ||
Assertions.assertEquals("Erik", result.get("name_")); | ||
|
||
// before fix, depth 2 does NOT work as expected | ||
Map<String, Object> addressMap = (Map<String, Object>) result.get("address_"); | ||
// before fix, null?? Fails here | ||
Assertions.assertEquals("Elvirastr", addressMap.get("street_")); | ||
Map<String, Object> cityMap = (Map<String, Object>) addressMap.get("city_"); | ||
Assertions.assertEquals(1, cityMap.get("id_")); | ||
Assertions.assertEquals("Berlin", cityMap.get("name_")); | ||
} | ||
|
||
} |