Ignore type in the MessageJacksonDeserializer

The `GenericJackson2JsonRedisSerializer` now resolves the target type
before calling `ObjectMapper.readValue()` causing a `ClassCast` in the
`StdNodeBasedDeserializer.deserializeWithType()`

* Override `MessageJacksonDeserializer.deserializeWithType()` with delegating
to the plain `deserialize()` ignoring the `TypeDeserializer`

Related to https://github.com/spring-projects/spring-data-redis/issues/2322
This commit is contained in:
Artem Bilan
2022-09-01 11:40:34 -04:00
parent ccf41d1480
commit 80eea1508e
2 changed files with 11 additions and 6 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2021 the original author or authors.
* Copyright 2017-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,11 +24,13 @@ import org.springframework.integration.support.MutableMessageHeaders;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.deser.std.StdNodeBasedDeserializer;
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
import com.fasterxml.jackson.databind.type.TypeFactory;
/**
@@ -66,6 +68,13 @@ public abstract class MessageJacksonDeserializer<T extends Message<?>> extends S
return this.mapper;
}
@Override
public Object deserializeWithType(JsonParser jp, DeserializationContext ctxt, TypeDeserializer td)
throws IOException {
return super.deserialize(jp, ctxt);
}
@Override
public T convert(JsonNode root, DeserializationContext ctxt) throws IOException {
Map<String, Object> headers = this.mapper.readValue(root.get("headers").traverse(),

View File

@@ -28,7 +28,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.JacksonObjectWriter;
import org.springframework.integration.IntegrationMessageHeaderAccessor;
import org.springframework.integration.channel.NullChannel;
import org.springframework.integration.history.MessageHistory;
@@ -185,10 +184,7 @@ class RedisChannelMessageStoreTests implements RedisContainerTest {
void testJsonSerialization() {
RedisChannelMessageStore store = new RedisChannelMessageStore(RedisContainerTest.connectionFactory());
ObjectMapper mapper = JacksonJsonUtils.messagingAwareMapper();
GenericJackson2JsonRedisSerializer serializer =
new GenericJackson2JsonRedisSerializer(mapper,
(mapper1, source, type) -> mapper1.readValue(source, Object.class),
JacksonObjectWriter.create());
GenericJackson2JsonRedisSerializer serializer = new GenericJackson2JsonRedisSerializer(mapper);
store.setValueSerializer(serializer);
Message<?> genericMessage = new GenericMessage<>(new Date());