spring-projects / spring-projects/spring-data-redis

`GenericJackson2JsonRedisSerializer` can't deserialize previously serialized `Stream.toList()`

Open
#2,697 10 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

for: team-attention type: bug
Dominant language
Java
Stars
1.9k
Forks
1.3k
PR merge metrics
No merged PRs in 30d

Description

Hi there,

I'm facing an issue with the GenericJackson2JsonRedisSerializer() when using a @Cacheable annotation on a method returning a List resulting of a Stream.toList() operation.

I can easily reproduce with the following unit test :

    List<Integer> toSerialize = Stream.of(2953).toList();

    @Test
    void test() {
        RedisSerializer<Object> serializer = RedisSerializer.json();

        var serialized = serializer.deserialize(serializer.serialize(new ArrayList<>(toSerialize))); // This is OK as the list is embedded in an ArrayList
        serialized = serializer.deserialize(serializer.serialize(toSerialize)); // EXCEPTION !!
    }

Please tell me if i'm doing something wrong.

SpringBoot version : 3.1.3

Stacktrace :

Caused by: com.fasterxml.jackson.databind.exc.InvalidTypeIdException: Could not resolve type id '2953' as a subtype of `java.lang.Object`: no such class found
 at [Source: (byte[])"[2953]"; line: 1, column: 6]
	at com.fasterxml.jackson.databind.exc.InvalidTypeIdException.from(InvalidTypeIdException.java:43)
	at com.fasterxml.jackson.databind.DeserializationContext.invalidTypeIdException(DeserializationContext.java:2084)
	at com.fasterxml.jackson.databind.DeserializationContext.handleUnknownTypeId(DeserializationContext.java:1575)
	at com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver._typeFromId(ClassNameIdResolver.java:76)
	at com.fasterxml.jackson.databind.jsontype.impl.ClassNameIdResolver.typeFromId(ClassNameIdResolver.java:66)
	at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:159)
	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer._deserialize(AsArrayTypeDeserializer.java:97)
	at com.fasterxml.jackson.databind.jsontype.impl.AsArrayTypeDeserializer.deserializeTypedFromArray(AsArrayTypeDeserializer.java:53)
	at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromAny(AsPropertyTypeDeserializer.java:238)
	at com.fasterxml.jackson.databind.deser.std.UntypedObjectDeserializerNR.deserializeWithType(UntypedObjectDeserializerNR.java:115)
	at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:74)
	at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:323)
	at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4825)
	at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3874)
	at org.springframework.data.redis.serializer.JacksonObjectReader.lambda$create$0(JacksonObjectReader.java:54)
	at org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer.deserialize(GenericJackson2JsonRedisSerializer.java:250)
	... 70 more

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied unit-test reproduction and GenericJackson2JsonRedisSerializer.deserialize, then compare serialization of Stream.toList() with ArrayList. Done means the previously serialized Stream.toList() List round-trips without InvalidTypeIdException, with coverage for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, redis
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.