-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fixed so that KeyDeserializer set by annotation is not overwritten by KeyDeserializer set in the mapper #4929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
d2a7c4a
a7538fb
355dcf6
094c63e
37ddcf9
2fe9822
c5f0a4b
e002abf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.fasterxml.jackson.databind.deser; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Even more specifically, Map-key deserialization tests are under There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can move the test. |
||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
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.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
// [databind#4444] | ||
public class TestKeyDeserializerOverwritten { | ||
@JsonDeserialize(keyUsing = ForClass.class) | ||
static class MyKey { | ||
private final String value; | ||
|
||
MyKey(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
static class ForClass extends KeyDeserializer { | ||
@Override | ||
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException { | ||
return new MyKey(key + "-class"); | ||
} | ||
} | ||
|
||
static class ForMapper extends KeyDeserializer { | ||
@Override | ||
public Object deserializeKey(String key, DeserializationContext ctxt) throws IOException { | ||
return new MyKey(key + "-mapper"); | ||
} | ||
} | ||
|
||
TypeReference<Map<MyKey, String>> typeRef = new TypeReference<>() {}; | ||
|
||
@Test | ||
void withoutForClass() throws JsonProcessingException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
ObjectMapper mapper = new ObjectMapper(); | ||
Map<MyKey, String> result = mapper.readValue("{\"foo\":null}", typeRef); | ||
|
||
assertEquals("foo-class", result.keySet().stream().findFirst().get().value); | ||
} | ||
|
||
// The KeyDeserializer set by the annotation must not be overwritten by the KeyDeserializer set in the mapper. | ||
@Test | ||
void withForClass() throws JsonProcessingException { | ||
SimpleModule sm = new SimpleModule(); | ||
sm.addKeyDeserializer(MyKey.class, new ForMapper()); | ||
|
||
ObjectMapper mapper = new ObjectMapper().registerModule(sm); | ||
Map<MyKey, String> result = mapper.readValue("{\"foo\":null}", typeRef); | ||
|
||
assertEquals("foo-class", result.keySet().stream().findFirst().get().value); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.