Skip to content

Fix DocumentReference resolution for properties used in constructor. #3810

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>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3806-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>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3806-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 @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3806-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 @@ -11,7 +11,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>3.3.0-SNAPSHOT</version>
<version>3.3.0-GH-3806-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1810,6 +1810,10 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
return (T) dbRefResolver.resolveDbRef(property, dbref, callback, dbRefProxyHandler);
}

if(property.isDocumentReference()) {
return (T) dbRefResolver.resolveReference(property, accessor.get(property), referenceLookupDelegate, context::convert);
}

return super.getPropertyValue(property);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,52 @@ void loadCollectionReferenceWithMissingRefs() {
assertThat(result.getSimpleValueRef()).containsExactly(new SimpleObjectRef("ref-2", "me-the-2-referenced-object"));
}

@Test // GH-3806
void resolveReferenceWhenUsedAsCtorArgument() {

Publisher publisher = new Publisher();
publisher.id = "p-111";
publisher.name = "ppp";

template.save(publisher);

WithRequiredArgsCtor source = new WithRequiredArgsCtor("id-1", publisher);

template.save(source);

WithRequiredArgsCtor target = template.findOne(query(where("id").is(source.id)), WithRequiredArgsCtor.class);
assertThat(target.publisher).isNotNull();
}

@Test // GH-3806
void resolveLazyReferenceWhenUsedAsCtorArgument() {

Publisher publisher = new Publisher();
publisher.id = "p-111";
publisher.name = "ppp";

template.save(publisher);

WithLazyRequiredArgsCtor source = new WithLazyRequiredArgsCtor("id-1", publisher);

template.save(source);

WithLazyRequiredArgsCtor target = template.findOne(query(where("id").is(source.id)), WithLazyRequiredArgsCtor.class);

// proxy not yet resolved
LazyLoadingTestUtils.assertProxy(target.publisher, (proxy) -> {

assertThat(proxy.isResolved()).isFalse();
assertThat(proxy.currentValue()).isNull();
});

// resolve the proxy by invoking a method on it
assertThat(target.getPublisher().getName()).isEqualTo("ppp");
LazyLoadingTestUtils.assertProxy(target.publisher, (proxy) -> {
assertThat(proxy.isResolved()).isTrue();
});
}

@Test // GH-3602
void queryForReference() {

Expand Down Expand Up @@ -1283,6 +1329,30 @@ static class Publisher {
String id;
String acronym;
String name;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getAcronym() {
return acronym;
}

public void setAcronym(String acronym) {
this.acronym = acronym;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

@Data
Expand All @@ -1293,4 +1363,40 @@ static class UsingAtReference {
@Reference //
Publisher publisher;
}

static class WithRequiredArgsCtor {

final String id;

@DocumentReference
final Publisher publisher;

public WithRequiredArgsCtor(String id, Publisher publisher) {

this.id = id;
this.publisher = publisher;
}
}

static class WithLazyRequiredArgsCtor {

final String id;

@DocumentReference(lazy = true)
final Publisher publisher;

public WithLazyRequiredArgsCtor(String id, Publisher publisher) {

this.id = id;
this.publisher = publisher;
}

public String getId() {
return id;
}

public Publisher getPublisher() {
return publisher;
}
}
}