-
-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d2a7c4a
Refactor the process of getting BeanDescription
k163377 a7538fb
Fixed to give priority to KeyDeserializer set by annotation
k163377 355dcf6
Add test for #4444
k163377 094c63e
Move test
k163377 37ddcf9
Resolves compile error in Java 8
k163377 2fe9822
Formatting import order
k163377 c5f0a4b
Merge branch '2.18' into key-deser
cowtowncoder e002abf
Rename test class, add release notes
cowtowncoder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
67 changes: 67 additions & 0 deletions
67
src/test/java/com/fasterxml/jackson/databind/deser/jdk/CustomMapKeyDeserializer4444Test.java
This file contains hidden or 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,67 @@ | ||
package com.fasterxml.jackson.databind.deser.jdk; | ||
|
||
import java.io.IOException; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
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 com.fasterxml.jackson.databind.testutil.DatabindTestUtil; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
// [databind#4444] | ||
public class CustomMapKeyDeserializer4444Test extends DatabindTestUtil | ||
{ | ||
@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"); | ||
} | ||
} | ||
|
||
// It is not declared as new TypeReference<> because it causes a compile error in Java 8. | ||
TypeReference<Map<MyKey, String>> typeRef = new TypeReference<Map<MyKey, String>>() { | ||
}; | ||
|
||
@Test | ||
void withoutForClass() throws Exception { | ||
ObjectMapper mapper = newJsonMapper(); | ||
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 Exception { | ||
SimpleModule sm = new SimpleModule(); | ||
sm.addKeyDeserializer(MyKey.class, new ForMapper()); | ||
|
||
ObjectMapper mapper = jsonMapperBuilder().addModule(sm).build(); | ||
Map<MyKey, String> result = mapper.readValue("{\"foo\":null}", typeRef); | ||
|
||
assertEquals("foo-class", result.keySet().stream().findFirst().get().value); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.