Skip to content

Commit 230ef8c

Browse files
DATAREDIS-471 - Hacking (CustomConversion / References).
Make sure custom conversions get applied. Handle References correctly.
1 parent c5e3dba commit 230ef8c

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,15 @@ protected void writePartialUpdate(PartialUpdate<?> update, RedisData sink) {
426426
}
427427

428428
writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType(), map, sink);
429-
}
429+
} else if (targetProperty.isAssociation()) {
430+
431+
KeyValuePersistentEntity<?> ref = mappingContext
432+
.getPersistentEntity(targetProperty.getAssociation().getInverse().getTypeInformation());
430433

431-
else {
434+
Object refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getIdProperty());
435+
436+
sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId));
437+
} else {
432438

433439
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(),
434440
targetProperty.getTypeInformation(), sink);

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,6 +1578,45 @@ public void writeShouldThrowExceptionOnPartialUpdatePathWithSimpleMapValueWhenIt
15781578
write(update);
15791579
}
15801580

1581+
/**
1582+
* @see DATAREDIS-471
1583+
*/
1584+
@Test
1585+
public void writeShouldWritePartialUpdatePathWithRegisteredCustomConversionCorrectly() {
1586+
1587+
this.converter = new MappingRedisConverter(null, null, resolverMock);
1588+
this.converter
1589+
.setCustomConversions(new CustomConversions(Collections.singletonList(new AddressToBytesConverter())));
1590+
this.converter.afterPropertiesSet();
1591+
1592+
Address address = new Address();
1593+
address.country = "Tel'aran'rhiod";
1594+
address.city = "unknown";
1595+
1596+
PartialUpdate<Person> update = new PartialUpdate<Person>("123", Person.class).set("address", address);
1597+
1598+
assertThat(write(update).getBucket(),
1599+
isBucket().containingUtf8String("address", "{\"city\":\"unknown\",\"country\":\"Tel'aran'rhiod\"}"));
1600+
}
1601+
1602+
/**
1603+
* @see DATAREDIS-425
1604+
*/
1605+
@Test
1606+
public void writeShouldWritePartialUpdatePathWithReferenceCorrectly() {
1607+
1608+
Location location = new Location();
1609+
location.id = "1";
1610+
location.name = "tar valon";
1611+
1612+
PartialUpdate<Person> update = new PartialUpdate<Person>("123", Person.class).set("location", location);
1613+
1614+
assertThat(write(update).getBucket(),
1615+
isBucket().containingUtf8String("location", "locations:1") //
1616+
.without("location.id") //
1617+
.without("location.name"));
1618+
}
1619+
15811620
private RedisData write(Object source) {
15821621

15831622
RedisData rdo = new RedisData();

0 commit comments

Comments
 (0)