Avoid duplicate context nesting to properly convert nested projections.
Closes: #4609 Original pull request: #4616
This commit is contained in:
committed by
Mark Paluch
parent
04adac45a9
commit
02655814b6
@@ -583,7 +583,6 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
}
|
||||
|
||||
ConversionContext propertyContext = context.forProperty(prop);
|
||||
MongoDbPropertyValueProvider valueProviderToUse = valueProvider.withContext(propertyContext);
|
||||
|
||||
if (prop.isAssociation()) {
|
||||
|
||||
@@ -611,7 +610,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
continue;
|
||||
}
|
||||
|
||||
accessor.setProperty(prop, valueProviderToUse.getPropertyValue(prop));
|
||||
accessor.setProperty(prop, valueProvider.getPropertyValue(prop));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2427,6 +2426,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
|
||||
this.returnedTypeDescriptor = projection;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public ConversionContext forProperty(String name) {
|
||||
|
||||
|
||||
@@ -2552,6 +2552,30 @@ public class MongoTemplateTests {
|
||||
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() {
|
||||
|
||||
|
||||
@@ -2709,6 +2709,44 @@ class MappingMongoConverterUnitTests {
|
||||
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() {
|
||||
|
||||
@@ -2958,6 +2996,7 @@ class MappingMongoConverterUnitTests {
|
||||
String lastname;
|
||||
|
||||
Set<Address> addresses;
|
||||
Address address;
|
||||
|
||||
Person() {
|
||||
|
||||
@@ -2981,6 +3020,16 @@ class MappingMongoConverterUnitTests {
|
||||
Set<AddressProjection> getAddresses();
|
||||
}
|
||||
|
||||
interface WithNestedInterfaceProjection {
|
||||
String getFirstname();
|
||||
AddressProjection getAddress();
|
||||
}
|
||||
|
||||
interface WithNestedDtoProjection {
|
||||
String getFirstname();
|
||||
AddressDto getAddress();
|
||||
}
|
||||
|
||||
interface ProjectionWithNestedEntity {
|
||||
|
||||
Set<Address> getAddresses();
|
||||
@@ -2991,6 +3040,19 @@ class MappingMongoConverterUnitTests {
|
||||
String getStreet();
|
||||
}
|
||||
|
||||
class AddressDto {
|
||||
|
||||
String street;
|
||||
|
||||
public String getStreet() {
|
||||
return street;
|
||||
}
|
||||
|
||||
public void setStreet(String street) {
|
||||
this.street = street;
|
||||
}
|
||||
}
|
||||
|
||||
static class PersonDto {
|
||||
|
||||
LocalDate birthDate;
|
||||
|
||||
Reference in New Issue
Block a user