Skip to content

Commit e3e73f5

Browse files
sxhinzvcchristophstrobl
authored andcommitted
Fix #self @DocumentReference resolution when used in constructor.
This commit enables document reference lookup to use `DocumentReferenceSource` to properly instantiate an entity containig a @DocumentReference `#self` property. Closes #4484 Original Pull Request: #4486
1 parent e5aff26 commit e3e73f5

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
* @author Roman Puchkovskiy
9999
* @author Heesu Jung
100100
* @author Divya Srivastava
101+
* @author Julia Lee
101102
*/
102103
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
103104

@@ -1976,8 +1977,9 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19761977
}
19771978

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

19831985
return super.getPropertyValue(property);

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
* {@link DocumentReference} related integration tests for {@link MongoTemplate}.
5858
*
5959
* @author Christoph Strobl
60+
* @author Julia Lee
6061
*/
6162
@ExtendWith(MongoClientExtension.class)
6263
public class MongoTemplateDocumentReferenceTests {
@@ -1265,6 +1266,32 @@ void readWriteTypeReferenceHavingFixedStringIdTargetType() {
12651266
.isEqualTo(new ObjectRefHavingStringIdTargetType(id.toHexString(), "me-the-referenced-object"));
12661267
}
12671268

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

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

0 commit comments

Comments
 (0)