Skip to content

Commit e3bb733

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 84b734c commit e3bb733

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
* @author Roman Puchkovskiy
118118
* @author Heesu Jung
119119
* @author Divya Srivastava
120+
* @author Julia Lee
120121
*/
121122
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
122123

@@ -1978,8 +1979,9 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19781979
}
19791980

19801981
if (property.isDocumentReference()) {
1981-
return (T) dbRefResolver.resolveReference(property, accessor.get(property), referenceLookupDelegate,
1982-
context::convert);
1982+
return (T) dbRefResolver.resolveReference(property,
1983+
new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)),
1984+
referenceLookupDelegate, context::convert);
19831985
}
19841986

19851987
return super.getPropertyValue(property);

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

+63
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
* {@link DocumentReference} related integration tests for {@link MongoTemplate}.
6363
*
6464
* @author Christoph Strobl
65+
* @author Julia Lee
6566
*/
6667
@ExtendWith(MongoClientExtension.class)
6768
public class MongoTemplateDocumentReferenceTests {
@@ -1270,6 +1271,32 @@ void readWriteTypeReferenceHavingFixedStringIdTargetType() {
12701271
.isEqualTo(new ObjectRefHavingStringIdTargetType(id.toHexString(), "me-the-referenced-object"));
12711272
}
12721273

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

@@ -1614,4 +1641,40 @@ public static class WithListOfRefs {
16141641

16151642
@DocumentReference private List<WithListOfRefs> refs;
16161643
}
1644+
1645+
static class OneToManyStylePublisherWithRequiredArgsCtor {
1646+
1647+
@Id
1648+
String id;
1649+
1650+
@ReadOnlyProperty
1651+
@DocumentReference(lookup="{'publisherId':?#{#self._id} }")
1652+
List<OneToManyStyleBook> books;
1653+
1654+
public OneToManyStylePublisherWithRequiredArgsCtor(String id, List<OneToManyStyleBook> books) {
1655+
this.id = id;
1656+
this.books = books;
1657+
}
1658+
1659+
public String getId() {
1660+
return this.id;
1661+
}
1662+
1663+
public List<OneToManyStyleBook> getBooks() {
1664+
return this.books;
1665+
}
1666+
1667+
public void setId(String id) {
1668+
this.id = id;
1669+
}
1670+
1671+
public void setBooks(List<OneToManyStyleBook> books) {
1672+
this.books = books;
1673+
}
1674+
1675+
public String toString() {
1676+
return "MongoTemplateDocumentReferenceTests.OneToManyStylePublisherWithRequiredArgsCtor(id=" + this.getId() + ", book="
1677+
+ this.getBooks() + ")";
1678+
}
1679+
}
16171680
}

0 commit comments

Comments
 (0)