Skip to content

Commit f66384a

Browse files
committed
Attempt early usage of custom converters.
We now try to eagerly apply a custom converter when reading Redis properties in the MappingRedisConverter before considering type hints from the hash. We require type hints to properly restore the target type for a hash entry as Redis data is all byte arrays, so a simple String needs a type hint before we can load it back into an e.g. Object-typed property. Subtypes of properties (such as ZoneRegion for ZoneId) are sometimes not associated with a converter as only the parent type (ZoneId) is associated with a converter. Trying to convert the value into the subtype directly through our ConversionService would fail in that case. Closes #2307
1 parent 98cb7ed commit f66384a

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/main/java/org/springframework/data/redis/core/convert/MappingRedisConverter.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,10 @@ protected Object readProperty(String path, RedisData source, RedisPersistentProp
323323
return null;
324324
}
325325

326+
if (customConversions.hasCustomReadTarget(byte[].class, persistentProperty.getType())) {
327+
return fromBytes(sourceBytes, persistentProperty.getType());
328+
}
329+
326330
Class<?> typeToUse = getTypeHint(currentPath, source.getBucket(), persistentProperty.getType());
327331
return fromBytes(sourceBytes, typeToUse);
328332
}
@@ -736,8 +740,7 @@ private void writeAssociation(String path, RedisPersistentEntity<?> entity, @Nul
736740
* @param sink
737741
*/
738742
private void writeCollection(@Nullable String keyspace, String path, @Nullable Iterable<?> values,
739-
TypeInformation<?> typeHint,
740-
RedisData sink) {
743+
TypeInformation<?> typeHint, RedisData sink) {
741744

742745
if (values == null) {
743746
return;

src/test/java/org/springframework/data/redis/core/convert/MappingRedisConverterUnitTests.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -696,11 +696,14 @@ void writesZoneIdValuesCorrectly() {
696696
assertThat(write(rand)).containsEntry("zoneId", "Europe/Paris");
697697
}
698698

699-
@Test // DATAREDIS-425
699+
@Test // DATAREDIS-425, GH-2307
700700
void readsZoneIdValuesCorrectly() {
701701

702-
Person target = converter.read(Person.class,
703-
new RedisData(Bucket.newBucketFromStringMap(Collections.singletonMap("zoneId", "Europe/Paris"))));
702+
Map<String, String> map = new HashMap<>();
703+
map.put("zoneId", "Europe/Paris");
704+
map.put("zoneId._class", "java.time.ZoneRegion");
705+
706+
Person target = converter.read(Person.class, new RedisData(Bucket.newBucketFromStringMap(map)));
704707

705708
assertThat(target.zoneId).isEqualTo(ZoneId.of("Europe/Paris"));
706709
}
@@ -1790,7 +1793,6 @@ void writeShouldThrowExceptionForUpdateCollectionValueNotAssignableToDomainTypeP
17901793
@Test // DATAREDIS-471
17911794
void writeShouldThrowExceptionForUpdateValueInCollectionNotAssignableToDomainTypeProperty() {
17921795

1793-
17941796
PartialUpdate<Person> update = new PartialUpdate<>("123", Person.class) //
17951797
.set("coworkers", Collections.singletonList("foo"));
17961798

@@ -1928,7 +1930,7 @@ void readEntityWithCustomConverter() {
19281930
// FIXME: https://github.com/spring-projects/spring-data-redis/issues/2168
19291931
void writePlainList() {
19301932

1931-
List<Object> source = Arrays.asList("Hello", "stream", "message", 100L);
1933+
List<Object> source = Arrays.asList("Hello", "stream", "message", 100L);
19321934
RedisTestData target = write(source);
19331935

19341936
assertThat(target).containsEntry("[0]", "Hello") //

0 commit comments

Comments
 (0)