Skip to content

Commit 22d51b8

Browse files
committed
Fix one-to-many lookup resolution for properties used in constructor.
This commit enables a source entity with a one-to-many @DocumentReference lookup property to be instantiated properly when the referenced entity - which contains the linking value - is used as a constructor argument. See #4484
1 parent fe8e57e commit 22d51b8

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1976,8 +1976,9 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19761976
}
19771977

19781978
if (property.isDocumentReference()) {
1979-
return (T) dbRefResolver.resolveReference(property, accessor.get(property), referenceLookupDelegate,
1980-
context::convert);
1979+
return (T) dbRefResolver.resolveReference(property,
1980+
new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)),
1981+
referenceLookupDelegate, context::convert);
19811982
}
19821983

19831984
return super.getPropertyValue(property);

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/MongoTemplateDocumentReferenceTests.java

+62
Original file line numberDiff line numberDiff line change
@@ -1265,6 +1265,32 @@ void readWriteTypeReferenceHavingFixedStringIdTargetType() {
12651265
.isEqualTo(new ObjectRefHavingStringIdTargetType(id.toHexString(), "me-the-referenced-object"));
12661266
}
12671267

1268+
@Test // GH-4484
1269+
void resolveReferenceForOneToManyLookupWithSelfVariableWhenUsedInCtorArgument() {
1270+
1271+
OneToManyStylePublisherWithRequiredArgsCtor publisher = new OneToManyStylePublisherWithRequiredArgsCtor("p-100", null);
1272+
template.save(publisher);
1273+
1274+
OneToManyStyleBook book1 = new OneToManyStyleBook();
1275+
book1.id = "id-1";
1276+
book1.publisherId = publisher.id;
1277+
1278+
OneToManyStyleBook book2 = new OneToManyStyleBook();
1279+
book2.id = "id-2";
1280+
book2.publisherId = "p-200";
1281+
1282+
OneToManyStyleBook book3 = new OneToManyStyleBook();
1283+
book3.id = "id-3";
1284+
book3.publisherId = publisher.id;
1285+
1286+
template.save(book1);
1287+
template.save(book2);
1288+
template.save(book3);
1289+
1290+
OneToManyStylePublisherWithRequiredArgsCtor target = template.findOne(query(where("id").is(publisher.id)), OneToManyStylePublisherWithRequiredArgsCtor.class);
1291+
assertThat(target.books).containsExactlyInAnyOrder(book1, book3);
1292+
}
1293+
12681294
static class SingleRefRoot {
12691295

12701296
String id;
@@ -2249,4 +2275,40 @@ public String toString() {
22492275
return "MongoTemplateDocumentReferenceTests.WithListOfRefs(id=" + this.getId() + ", refs=" + this.getRefs() + ")";
22502276
}
22512277
}
2278+
2279+
static class OneToManyStylePublisherWithRequiredArgsCtor {
2280+
2281+
@Id
2282+
String id;
2283+
2284+
@ReadOnlyProperty
2285+
@DocumentReference(lookup="{'publisherId':?#{#self._id} }")
2286+
List<OneToManyStyleBook> books;
2287+
2288+
public OneToManyStylePublisherWithRequiredArgsCtor(String id, List<OneToManyStyleBook> books) {
2289+
this.id = id;
2290+
this.books = books;
2291+
}
2292+
2293+
public String getId() {
2294+
return this.id;
2295+
}
2296+
2297+
public List<OneToManyStyleBook> getBooks() {
2298+
return this.books;
2299+
}
2300+
2301+
public void setId(String id) {
2302+
this.id = id;
2303+
}
2304+
2305+
public void setBooks(List<OneToManyStyleBook> books) {
2306+
this.books = books;
2307+
}
2308+
2309+
public String toString() {
2310+
return "MongoTemplateDocumentReferenceTests.OneToManyStylePublisherWithRequiredArgsCtor(id=" + this.getId() + ", book="
2311+
+ this.getBooks() + ")";
2312+
}
2313+
}
22522314
}

0 commit comments

Comments
 (0)