Browse Source

Correctly read unwrapped properties during constructor creation.

Closes: #4491
Original Pull Request: #4492
4.0.x
Mark Paluch 2 years ago committed by Christoph Strobl
parent
commit
531d3ac096
No known key found for this signature in database
GPG Key ID: 8CC1AB53391458C8
  1. 13
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  2. 30
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

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

@ -1963,6 +1963,8 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> T getPropertyValue(MongoPersistentProperty property) { public <T> T getPropertyValue(MongoPersistentProperty property) {
ConversionContext propertyContext = context.forProperty(property);
if (property.isDbReference() && property.getDBRef().lazy()) { if (property.isDbReference() && property.getDBRef().lazy()) {
Object rawRefValue = accessor.get(property); Object rawRefValue = accessor.get(property);
@ -1979,9 +1981,16 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
} }
if (property.isDocumentReference()) { if (property.isDocumentReference()) {
return (T) dbRefResolver.resolveReference(property, return (T) dbRefResolver.resolveReference(property,
new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)), new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)), referenceLookupDelegate,
referenceLookupDelegate, context::convert); context::convert);
}
if (property.isUnwrapped()) {
return (T) readUnwrapped(propertyContext, accessor, property,
mappingContext.getRequiredPersistentEntity(property));
} }
return super.getPropertyValue(property); return super.getPropertyValue(property);

30
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/MappingMongoConverterUnitTests.java

@ -2350,6 +2350,24 @@ class MappingMongoConverterUnitTests {
.isEqualTo(expected); .isEqualTo(expected);
} }
@Test // GH-4491
void readUnwrappedTypeWithComplexValueUsingConstructor() {
org.bson.Document source = new org.bson.Document("_id", "id-1").append("stringValue", "hello").append("address",
new org.bson.Document("s", "1007 Mountain Drive").append("city", "Gotham"));
WithUnwrappedConstructor target = converter.read(WithUnwrappedConstructor.class, source);
Address expected = new Address();
expected.city = "Gotham";
expected.street = "1007 Mountain Drive";
assertThat(target.embeddableValue.stringValue) //
.isEqualTo("hello");
assertThat(target.embeddableValue.address) //
.isEqualTo(expected);
}
@Test // DATAMONGO-1902 @Test // DATAMONGO-1902
void writeUnwrappedTypeWithComplexValue() { void writeUnwrappedTypeWithComplexValue() {
@ -3374,6 +3392,18 @@ class MappingMongoConverterUnitTests {
@Unwrapped.Nullable EmbeddableType embeddableValue; @Unwrapped.Nullable EmbeddableType embeddableValue;
} }
static class WithUnwrappedConstructor {
private final String id;
private final @Unwrapped.Empty EmbeddableType embeddableValue;
public WithUnwrappedConstructor(String id, EmbeddableType embeddableValue) {
this.id = id;
this.embeddableValue = embeddableValue;
}
}
static class WithPrefixedNullableUnwrapped { static class WithPrefixedNullableUnwrapped {
String id; String id;

Loading…
Cancel
Save