Skip to content

StreamOperations.map(…) does not retain target type hint #2198

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

Closed
huan1993 opened this issue Nov 10, 2021 · 3 comments
Closed

StreamOperations.map(…) does not retain target type hint #2198

huan1993 opened this issue Nov 10, 2021 · 3 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@huan1993
Copy link

huan1993 commented Nov 10, 2021

description

Today, when I read a message of type ObjectRecord from Redis Stream, the following exception occurred. I don't know how to solve it.

error info

2021-11-10 14:05:11.457  INFO 8754 --- [           main] c.h.study.redis.RedisStreamApplication   : Started RedisStreamApplication in 2.598 seconds (JVM running for 3.399)
2021-11-10 14:05:11.566  INFO 8754 --- [           main] c.h.s.r.stream.producer.StreamProducer   : generator book info :[Book(title=Time To Murder And Create, author=Vito Denesik)]
2021-11-10 14:05:11.662  INFO 8754 --- [           main] c.h.s.r.stream.producer.StreamProducer   : return record-id:[1636524311682-0]
Exception in thread "xread-nonblock-02" java.lang.IllegalArgumentException: Value must not be null!
	at org.springframework.util.Assert.notNull(Assert.java:201)
	at org.springframework.data.redis.connection.stream.Record.of(Record.java:81)
	at org.springframework.data.redis.connection.stream.MapRecord.toObjectRecord(MapRecord.java:147)
	at org.springframework.data.redis.core.StreamObjectMapper.toObjectRecord(StreamObjectMapper.java:138)
	at org.springframework.data.redis.core.StreamObjectMapper.toObjectRecords(StreamObjectMapper.java:164)
	at org.springframework.data.redis.core.StreamOperations.map(StreamOperations.java:594)
	at org.springframework.data.redis.core.StreamOperations.read(StreamOperations.java:413)
	at com.huan.study.redis.stream.consumer.xread.XreadNonBlockConsumer02.lambda$afterPropertiesSet$1(XreadNonBlockConsumer02.java:62)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)

core code

spring data stream error

@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
    redisTemplate.setConnectionFactory(connectionFactory);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new StringRedisSerializer());
    redisTemplate.setHashKeySerializer(new StringRedisSerializer());
    redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(Object.class));
    return redisTemplate;
}

The code is very easy, that is, the producer generates an ObjectRecord type message, and then the consumer consumes the message.

The following is a complete example to reproduce this problem

https://github.com/huan1993/spring-data-redis-stream

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 10, 2021
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 11, 2021
@mp911de mp911de changed the title read ObjectRecord stream message error StreamOperations.map(…) cannot decode object records Nov 11, 2021
@mp911de
Copy link
Member

mp911de commented Nov 11, 2021

There are two issues coming together. First, Jackson2JsonRedisSerializer serializes strings into a Base64 representation. It doesn't deserialize Base64 back into a String, therefore the type hint is lost.

Second, StreamOperations.map(…) does not propagate the type hint into the ObjectHashMapper which is unfortunate. Those both aspects together cause the issue. Please use the regular StringRedisSerializer for the time being as hash value serializer.

@mp911de mp911de self-assigned this Nov 11, 2021
@mp911de mp911de changed the title StreamOperations.map(…) cannot decode object records StreamOperations.map(…) does not retain target type hint Nov 11, 2021
@huan1993
Copy link
Author

I tried it, use StringRedisSerializer program can run normally.
thanks.

@mp911de mp911de reopened this Nov 12, 2021
@mp911de
Copy link
Member

mp911de commented Nov 12, 2021

Let's keep this one open as we still need to fix the type hint propagation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants