Skip to content

Fix nested projection retrieval #4616

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 5 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.2.3-SNAPSHOT</version>
<version>4.2.x-4609-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.2.3-SNAPSHOT</version>
<version>4.2.x-4609-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.2.3-SNAPSHOT</version>
<version>4.2.x-4609-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 @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.2.3-SNAPSHOT</version>
<version>4.2.x-4609-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
}

ConversionContext propertyContext = context.forProperty(prop);
MongoDbPropertyValueProvider valueProviderToUse = valueProvider.withContext(propertyContext);

if (prop.isAssociation()) {

Expand Down Expand Up @@ -623,7 +622,7 @@ private void readProperties(ConversionContext context, MongoPersistentEntity<?>
continue;
}

accessor.setProperty(prop, valueProviderToUse.getPropertyValue(prop));
accessor.setProperty(prop, valueProvider.getPropertyValue(prop));
}
}

Expand Down Expand Up @@ -2436,6 +2435,8 @@ class ProjectingConversionContext extends DefaultConversionContext {
this.returnedTypeDescriptor = projection;
}



@Override
public ConversionContext forProperty(String name) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2554,6 +2554,30 @@ public void findAndReplaceShouldProjectReturnedObjectCorrectly() {
assertThat(projection.getName()).isEqualTo("Walter");
}

@Test // GH-4609
public void shouldReadNestedProjection() {

MyPerson walter = new MyPerson("Walter");
walter.address = new Address("spring", "data");
template.save(walter);

PersonPWA result = template.query(MyPerson.class)
.as(PersonPWA.class)
.matching(where("id").is(walter.id))
.firstValue();

assertThat(result.getAddress().getCity()).isEqualTo("data");
}

interface PersonPWA {
String getName();
AdressProjection getAddress();
}

interface AdressProjection {
String getCity();
}

@Test // GH-4300
public void findAndReplaceShouldAllowNativeDomainTypesAndReturnAProjection() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2851,6 +2851,44 @@ void projectShouldReadNestedProjection() {
assertThat(person.getAddresses()).extracting(AddressProjection::getStreet).hasSize(1).containsOnly("hwy");
}

@Test // GH-4609
void projectShouldReadNestedInterfaceProjection() {

org.bson.Document source = new org.bson.Document("foo", "spring").append("address",
new org.bson.Document("s", "data").append("city", "mongodb"));

EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(),
EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy()
.and((target, underlyingType) -> !converter.conversions.isSimpleType(target)),
mappingContext);

EntityProjection<WithNestedInterfaceProjection, Person> projection = introspector.introspect(WithNestedInterfaceProjection.class,
Person.class);
WithNestedInterfaceProjection person = converter.project(projection, source);

assertThat(person.getFirstname()).isEqualTo("spring");
assertThat(person.getAddress().getStreet()).isEqualTo("data");
}

@Test // GH-4609
void projectShouldReadNestedDtoProjection() {

org.bson.Document source = new org.bson.Document("foo", "spring").append("address",
new org.bson.Document("s", "data").append("city", "mongodb"));

EntityProjectionIntrospector introspector = EntityProjectionIntrospector.create(converter.getProjectionFactory(),
EntityProjectionIntrospector.ProjectionPredicate.typeHierarchy()
.and((target, underlyingType) -> !converter.conversions.isSimpleType(target)),
mappingContext);

EntityProjection<WithNestedDtoProjection, Person> projection = introspector.introspect(WithNestedDtoProjection.class,
Person.class);
WithNestedDtoProjection person = converter.project(projection, source);

assertThat(person.getFirstname()).isEqualTo("spring");
assertThat(person.getAddress().getStreet()).isEqualTo("data");
}

@Test // GH-2860
void projectShouldReadProjectionWithNestedEntity() {

Expand Down Expand Up @@ -3206,6 +3244,7 @@ static class Person implements Contact {
String lastname;

Set<Address> addresses;
Address address;

Person() {

Expand Down Expand Up @@ -3248,6 +3287,16 @@ interface WithNestedProjection {
Set<AddressProjection> getAddresses();
}

interface WithNestedInterfaceProjection {
String getFirstname();
AddressProjection getAddress();
}

interface WithNestedDtoProjection {
String getFirstname();
AddressDto getAddress();
}

interface ProjectionWithNestedEntity {

Set<Address> getAddresses();
Expand All @@ -3258,6 +3307,19 @@ interface AddressProjection {
String getStreet();
}

class AddressDto {

String street;

public String getStreet() {
return street;
}

public void setStreet(String street) {
this.street = street;
}
}

static class PersonDto {

LocalDate birthDate;
Expand Down