Skip to content

Commit 59065ab

Browse files
christophstroblmp911de
authored andcommitted
DATAREDIS-1179 - Allow reuse of RedisConverter in ObjectHashMapper.
Original pull request: spring-projects#548.
1 parent f92ed76 commit 59065ab

File tree

3 files changed

+81
-7
lines changed

3 files changed

+81
-7
lines changed

src/main/asciidoc/reference/redis-streams.adoc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,37 @@ redisTemplate()
293293
.add(record); <1>
294294
----
295295
<1> XADD user-logon * "firstname" "night" "@class" "com.example.User" "lastname" "angel"
296+
297+
[NOTE]
298+
====
299+
A `StreamMessageListenerContainer` may not be aware of any `@TypeAlias` used on domain types as those need to be resolved via a `MappingContext`. So please make sure to pre initialize the `RedisMappingContext` with the `initialEntitySet`.
300+
301+
[source,java]
302+
----
303+
@Bean
304+
RedisMappingContext redisMappingContext() {
305+
RedisMappingContext ctx = new RedisMappingContext();
306+
ctx.setInitialEntitySet(Collections.singleton(Person.class));
307+
return ctx;
308+
}
309+
310+
@Bean
311+
RedisConverter redisConverter(RedisMappingContext mappingContext) {
312+
return new MappingRedisConverter(mappingContext, null, null);
313+
}
314+
315+
@Bean
316+
ObjectHashMapper hashMapper(RedisConverter converter) {
317+
return new ObjectHashMapper(converter);
318+
}
319+
320+
@Bean
321+
StreamMessageListenerContainer streamMessageListenerContainer(RedisConnectionFactory connectionFactory, ObjectHashMapper hashMapper) {
322+
StreamMessageListenerContainerOptions<String, ObjectRecord<String, Object>> options = StreamMessageListenerContainerOptions.builder()
323+
.objectMapper(hashMapper)
324+
.build();
325+
326+
return StreamMessageListenerContainer.create(connectionFactory, options);
327+
}
328+
----
329+
====

src/main/java/org/springframework/data/redis/hash/ObjectHashMapper.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,21 @@
1616
package org.springframework.data.redis.hash;
1717

1818
import java.util.Collections;
19-
import java.util.LinkedHashMap;
2019
import java.util.Map;
2120
import java.util.Set;
2221

23-
import org.springframework.core.convert.ConverterNotFoundException;
24-
import org.springframework.data.mapping.PropertyPath;
25-
import org.springframework.data.mapping.PropertyReferenceException;
2622
import org.springframework.data.redis.core.convert.CustomConversions;
2723
import org.springframework.data.redis.core.convert.IndexResolver;
2824
import org.springframework.data.redis.core.convert.IndexedData;
2925
import org.springframework.data.redis.core.convert.MappingRedisConverter;
26+
import org.springframework.data.redis.core.convert.RedisConverter;
3027
import org.springframework.data.redis.core.convert.RedisCustomConversions;
3128
import org.springframework.data.redis.core.convert.RedisData;
3229
import org.springframework.data.redis.core.convert.ReferenceResolver;
3330
import org.springframework.data.redis.core.mapping.RedisMappingContext;
34-
import org.springframework.data.redis.core.mapping.RedisPersistentEntity;
3531
import org.springframework.data.util.TypeInformation;
3632
import org.springframework.lang.Nullable;
33+
import org.springframework.util.Assert;
3734

3835
/**
3936
* {@link HashMapper} based on {@link MappingRedisConverter}. Supports nested properties and simple types like
@@ -74,7 +71,7 @@
7471
*/
7572
public class ObjectHashMapper implements HashMapper<Object, byte[], byte[]> {
7673

77-
private final MappingRedisConverter converter;
74+
private final RedisConverter converter;
7875

7976
/**
8077
* Creates new {@link ObjectHashMapper}.
@@ -110,6 +107,19 @@ public ObjectHashMapper(@Nullable org.springframework.data.convert.CustomConvers
110107
converter = mappingConverter;
111108
}
112109

110+
/**
111+
* Creates a new {@link ObjectHashMapper} using the given {@link RedisConverter} for conversion.
112+
*
113+
* @param converter must not be {@literal null}.
114+
* @throws IllegalArgumentException if the given {@literal converter} is {@literal null}.
115+
* @since 2.4
116+
*/
117+
public ObjectHashMapper(RedisConverter converter) {
118+
119+
Assert.notNull(converter, "Converter must not be null!");
120+
this.converter = converter;
121+
}
122+
113123
/*
114124
* (non-Javadoc)
115125
* @see org.springframework.data.redis.hash.HashMapper#toHash(java.lang.Object)

src/test/java/org/springframework/data/redis/mapping/ObjectHashMapperTests.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,15 @@
1717

1818
import static org.assertj.core.api.Assertions.*;
1919

20+
import lombok.Data;
21+
22+
import java.util.Collections;
2023
import java.util.Map;
2124

2225
import org.junit.Test;
23-
26+
import org.springframework.data.annotation.TypeAlias;
27+
import org.springframework.data.redis.core.convert.MappingRedisConverter;
28+
import org.springframework.data.redis.core.mapping.RedisMappingContext;
2429
import org.springframework.data.redis.hash.ObjectHashMapper;
2530

2631
/**
@@ -57,4 +62,29 @@ public void fromHashShouldFailIfTypeDoesNotMatch() {
5762

5863
String result = objectHashMapper.fromHash(hash, String.class);
5964
}
65+
66+
@Test // DATAREDIS-1179
67+
public void hashMapperAllowsReuseOfRedisConverter/*and thus the MappingContext holding eg. TypeAlias information*/() {
68+
69+
WithTypeAlias source = new WithTypeAlias();
70+
source.value = "val";
71+
Map<byte[], byte[]> hash = new ObjectHashMapper().toHash(source);
72+
73+
RedisMappingContext ctx = new RedisMappingContext();
74+
ctx.setInitialEntitySet(Collections.singleton(WithTypeAlias.class));
75+
ctx.afterPropertiesSet();
76+
77+
MappingRedisConverter mappingRedisConverter = new MappingRedisConverter(ctx, null, null);
78+
mappingRedisConverter.afterPropertiesSet();
79+
80+
ObjectHashMapper objectHashMapper = new ObjectHashMapper(mappingRedisConverter);
81+
assertThat(objectHashMapper.fromHash(hash)).isEqualTo(source);
82+
}
83+
84+
@TypeAlias("_42_")
85+
@Data
86+
static class WithTypeAlias {
87+
String value;
88+
}
89+
6090
}

0 commit comments

Comments
 (0)