Skip to content

Commit

Permalink
Ignore type in the MessageJacksonDeserializer
Browse files Browse the repository at this point in the history
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 spring-projects/spring-data-redis#2322
  • Loading branch information
artembilan committed Sep 1, 2022
1 parent ccf41d1 commit 80eea15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,11 +24,13 @@
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;

/**
Expand Down Expand Up @@ -66,6 +68,13 @@ protected ObjectMapper getMapper() {
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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@

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;
Expand Down Expand Up @@ -185,10 +184,7 @@ void testPriority() {
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());
Expand Down

0 comments on commit 80eea15

Please sign in to comment.