Skip to content
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

Fixed so that KeyDeserializer set by annotation is not overwritten by KeyDeserializer set in the mapper #4929

Merged
merged 8 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -1268,10 +1268,12 @@ public KeyDeserializer createKeyDeserializer(DeserializationContext ctxt,
throws JsonMappingException
{
final DeserializationConfig config = ctxt.getConfig();
BeanDescription beanDesc = null;
KeyDeserializer deser = null;
if (_factoryConfig.hasKeyDeserializers()) {
beanDesc = config.introspectClassAnnotations(type);
final BeanDescription beanDesc = config.introspectClassAnnotations(type);

// [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 @@ -1282,17 +1284,10 @@ public KeyDeserializer createKeyDeserializer(DeserializationContext ctxt,

// the only non-standard thing is this:
if (deser == null) {
// [databind#2452]: Support `@JsonDeserialize(keyUsing = ...)`
if (beanDesc == null) {
beanDesc = config.introspectClassAnnotations(type.getRawClass());
}
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,63 @@
package com.fasterxml.jackson.databind.ser;

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 {
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);
}
}
Loading