Skip to content

Commit 0eb2faa

Browse files
DATAREDIS-471 - Hacking (References in Lists).
Fix handling of references in lists.
1 parent 230ef8c commit 0eb2faa

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

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

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -405,35 +405,53 @@ protected void writePartialUpdate(PartialUpdate<?> update, RedisData sink) {
405405
ClassTypeInformation.OBJECT, sink);
406406
continue;
407407
}
408-
if (targetProperty.isCollectionLike()) {
409408

410-
Collection<Object> c = pUpdate.getValue() instanceof Collection ? (Collection<Object>) pUpdate.getValue()
409+
if (targetProperty.isAssociation()) {
410+
411+
if (targetProperty.isCollectionLike()) {
412+
413+
KeyValuePersistentEntity<?> ref = mappingContext.getPersistentEntity(
414+
targetProperty.getAssociation().getInverse().getTypeInformation().getComponentType().getActualType());
415+
416+
int i = 0;
417+
for (Object o : (Collection<?>) pUpdate.getValue()) {
418+
419+
Object refId = ref.getPropertyAccessor(o).getProperty(ref.getIdProperty());
420+
sink.getBucket().put(pUpdate.getPropertyPath() + ".[" + i + "]",
421+
toBytes(ref.getKeySpace() + ":" + refId));
422+
i++;
423+
}
424+
} else {
425+
426+
KeyValuePersistentEntity<?> ref = mappingContext
427+
.getPersistentEntity(targetProperty.getAssociation().getInverse().getTypeInformation());
428+
429+
Object refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getIdProperty());
430+
sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId));
431+
}
432+
}
433+
434+
else if (targetProperty.isCollectionLike()) {
435+
436+
Collection<?> collection = pUpdate.getValue() instanceof Collection ? (Collection<?>) pUpdate.getValue()
411437
: Collections.<Object> singleton(pUpdate.getValue());
412-
writeCollection(entity.getKeySpace(), pUpdate.getPropertyPath(), c, targetProperty.getTypeInformation(),
413-
sink);
438+
writeCollection(entity.getKeySpace(), pUpdate.getPropertyPath(), collection,
439+
targetProperty.getTypeInformation(), sink);
414440
} else if (targetProperty.isMap()) {
415441

416442
Map<Object, Object> map = new HashMap<Object, Object>();
417443

418444
if (pUpdate.getValue() instanceof Map) {
419-
map.putAll((Map) pUpdate.getValue());
445+
map.putAll((Map<?, ?>) pUpdate.getValue());
420446
} else if (pUpdate.getValue() instanceof Map.Entry) {
421-
map.put(((Map.Entry) pUpdate.getValue()).getKey(), ((Map.Entry) pUpdate.getValue()).getValue());
447+
map.put(((Map.Entry<?, ?>) pUpdate.getValue()).getKey(), ((Map.Entry<?, ?>) pUpdate.getValue()).getValue());
422448
} else {
423449
throw new MappingException(
424450
String.format("Cannot set update value for map property '%s' to '%s'. Please use a Map or Map.Entry.",
425451
pUpdate.getPropertyPath(), pUpdate.getValue()));
426452
}
427453

428454
writeMap(entity.getKeySpace(), pUpdate.getPropertyPath(), targetProperty.getMapValueType(), map, sink);
429-
} else if (targetProperty.isAssociation()) {
430-
431-
KeyValuePersistentEntity<?> ref = mappingContext
432-
.getPersistentEntity(targetProperty.getAssociation().getInverse().getTypeInformation());
433-
434-
Object refId = ref.getPropertyAccessor(pUpdate.getValue()).getProperty(ref.getIdProperty());
435-
436-
sink.getBucket().put(pUpdate.getPropertyPath(), toBytes(ref.getKeySpace() + ":" + refId));
437455
} else {
438456

439457
writeInternal(entity.getKeySpace(), pUpdate.getPropertyPath(), pUpdate.getValue(),

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,29 @@ public void writeShouldWritePartialUpdatePathWithRegisteredCustomConversionCorre
16051605
@Test
16061606
public void writeShouldWritePartialUpdatePathWithReferenceCorrectly() {
16071607

1608+
Location tar = new Location();
1609+
tar.id = "1";
1610+
tar.name = "tar valon";
1611+
1612+
Location tear = new Location();
1613+
tear.id = "2";
1614+
tear.name = "city of tear";
1615+
1616+
PartialUpdate<Person> update = new PartialUpdate<Person>("123", Person.class).set("visited",
1617+
Arrays.asList(tar, tear));
1618+
1619+
assertThat(write(update).getBucket(),
1620+
isBucket().containingUtf8String("visited.[0]", "locations:1").containingUtf8String("visited.[1]", "locations:2") //
1621+
.without("visited.id") //
1622+
.without("visited.name"));
1623+
}
1624+
1625+
/**
1626+
* @see DATAREDIS-425
1627+
*/
1628+
@Test
1629+
public void writeShouldWritePartialUpdatePathWithListOfReferencesCorrectly() {
1630+
16081631
Location location = new Location();
16091632
location.id = "1";
16101633
location.name = "tar valon";

0 commit comments

Comments
 (0)