Skip to content

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 8 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -1850,6 +1850,10 @@ wrongwrong (@k163377)
* Reported #4878: When serializing a Map via Converter(StdDelegatingSerializer),
a NullPointerException is thrown due to missing key serializer
(2.18.3)
* Contributed fix for #4444: The `KeyDeserializer` specified in the class with
`@JsonDeserialize(keyUsing = ...)` is overwritten by the `KeyDeserializer`
specified in the `ObjectMapper`.
(2.18.3)

Bernd Ahlers (@bernd)
* Reported #4742: Deserialization with Builder, External type id, `@JsonCreator` failing
Expand Down
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Project: jackson-databind

2.18.3 (not yet released)

#4444: The `KeyDeserializer` specified in the class with `@JsonDeserialize(keyUsing = ...)`
is overwritten by the `KeyDeserializer` specified in the `ObjectMapper`.
(fix by @wrongwrong)
#4827: Subclassed Throwable deserialization fails since v2.18.0 - no creator
index for property 'cause'
(reported by @nilswieber)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1269,8 +1269,11 @@ public KeyDeserializer createKeyDeserializer(DeserializationContext ctxt,
{
final DeserializationConfig config = ctxt.getConfig();
final BeanDescription beanDesc = config.introspectClassAnnotations(type);
KeyDeserializer deser = null;
if (_factoryConfig.hasKeyDeserializers()) {

// [databind#2452]: Support `@JsonDeserialize(keyUsing = ...)`
KeyDeserializer deser = findKeyDeserializerFromAnnotation(ctxt, beanDesc.getClassInfo());

if (deser == null && _factoryConfig.hasKeyDeserializers()) {
for (KeyDeserializers d : _factoryConfig.keyDeserializers()) {
deser = d.findKeyDeserializer(type, config, beanDesc);
if (deser != null) {
Expand All @@ -1281,14 +1284,10 @@ public KeyDeserializer createKeyDeserializer(DeserializationContext ctxt,

// the only non-standard thing is this:
if (deser == null) {
// [databind#2452]: Support `@JsonDeserialize(keyUsing = ...)`
deser = findKeyDeserializerFromAnnotation(ctxt, beanDesc.getClassInfo());
if (deser == null) {
if (type.isEnumType()) {
deser = _createEnumKeyDeserializer(ctxt, type);
} else {
deser = StdKeyDeserializers.findStringBasedKeyDeserializer(config, type);
}
if (type.isEnumType()) {
deser = _createEnumKeyDeserializer(ctxt, type);
} else {
deser = StdKeyDeserializers.findStringBasedKeyDeserializer(config, type);
}
}
// and then post-processing
Expand Down
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);
}
}
Loading