Skip to content

Fix issue with reference conversion in updates. #4045

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4041-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4041-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4041-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-4041-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/
package org.springframework.data.mongodb.core.convert;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map.Entry;

import org.bson.Document;
Expand All @@ -34,6 +36,7 @@
import org.springframework.data.util.ClassTypeInformation;
import org.springframework.data.util.TypeInformation;
import org.springframework.lang.Nullable;
import org.springframework.util.ObjectUtils;

/**
* A subclass of {@link QueryMapper} that retains type information on the mongo types.
Expand Down Expand Up @@ -209,8 +212,18 @@ private Object getMappedModifier(@Nullable Field field, Modifier modifier) {
: getMappedSort(sortObject, field.getPropertyEntity());
}

TypeInformation<?> typeHint = field == null ? ClassTypeInformation.OBJECT : field.getTypeHint();
if (isAssociationConversionNecessary(field, value)) {
if (ObjectUtils.isArray(value) || value instanceof Collection) {
List<Object> targetPointers = new ArrayList<>();
for (Object val : converter.getConversionService().convert(value, List.class)) {
targetPointers.add(getMappedValue(field, val));
}
return targetPointers;
}
return super.getMappedValue(field, value);
}

TypeInformation<?> typeHint = field == null ? ClassTypeInformation.OBJECT : field.getTypeHint();
return converter.convertToMongoType(value, typeHint);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,29 @@ void updateReferenceWithValue() {
assertThat(target).containsEntry("toB", "b");
}

@Test // GH-4041
void updateReferenceWithPushToCollection() {

WithListOfRefs a = new WithListOfRefs();
a.id = "a";
template.save(a);

WithListOfRefs b = new WithListOfRefs();
b.id = "b";
template.save(b);

template.update(WithListOfRefs.class).matching(where("id").is(a.id))
.apply(new Update().push("refs").each(new Object[] { b })).first();

String collection = template.getCollectionName(WithListOfRefs.class);

Document target = template.execute(db -> {
return db.getCollection(collection).find(Filters.eq("_id", "a")).first();
});

assertThat(target).containsEntry("refs", Collections.singletonList("b"));
}

@Test // GH-3782
void updateReferenceHavingCustomizedIdTargetType() {

Expand Down Expand Up @@ -1584,4 +1607,11 @@ public Publisher getPublisher() {
return publisher;
}
}

@Data
public static class WithListOfRefs {
@Id private String id;

@DocumentReference private List<WithListOfRefs> refs;
}
}