Skip to content

Commit 531d3ac

Browse files
mp911dechristophstrobl
authored andcommitted
Correctly read unwrapped properties during constructor creation.
Closes: #4491 Original Pull Request: #4492
1 parent e3bb733 commit 531d3ac

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,8 @@ class AssociationAwareMongoDbPropertyValueProvider extends MongoDbPropertyValueP
19631963
@SuppressWarnings("unchecked")
19641964
public <T> T getPropertyValue(MongoPersistentProperty property) {
19651965

1966+
ConversionContext propertyContext = context.forProperty(property);
1967+
19661968
if (property.isDbReference() && property.getDBRef().lazy()) {
19671969

19681970
Object rawRefValue = accessor.get(property);
@@ -1979,9 +1981,16 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19791981
}
19801982

19811983
if (property.isDocumentReference()) {
1984+
19821985
return (T) dbRefResolver.resolveReference(property,
1983-
new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)),
1984-
referenceLookupDelegate, context::convert);
1986+
new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)), referenceLookupDelegate,
1987+
context::convert);
1988+
}
1989+
1990+
if (property.isUnwrapped()) {
1991+
1992+
return (T) readUnwrapped(propertyContext, accessor, property,
1993+
mappingContext.getRequiredPersistentEntity(property));
19851994
}
19861995

19871996
return super.getPropertyValue(property);

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

+30
Original file line numberDiff line numberDiff line change
@@ -2350,6 +2350,24 @@ void readUnwrappedTypeWithComplexValue() {
23502350
.isEqualTo(expected);
23512351
}
23522352

2353+
@Test // GH-4491
2354+
void readUnwrappedTypeWithComplexValueUsingConstructor() {
2355+
2356+
org.bson.Document source = new org.bson.Document("_id", "id-1").append("stringValue", "hello").append("address",
2357+
new org.bson.Document("s", "1007 Mountain Drive").append("city", "Gotham"));
2358+
2359+
WithUnwrappedConstructor target = converter.read(WithUnwrappedConstructor.class, source);
2360+
2361+
Address expected = new Address();
2362+
expected.city = "Gotham";
2363+
expected.street = "1007 Mountain Drive";
2364+
2365+
assertThat(target.embeddableValue.stringValue) //
2366+
.isEqualTo("hello");
2367+
assertThat(target.embeddableValue.address) //
2368+
.isEqualTo(expected);
2369+
}
2370+
23532371
@Test // DATAMONGO-1902
23542372
void writeUnwrappedTypeWithComplexValue() {
23552373

@@ -3374,6 +3392,18 @@ static class WithNullableUnwrapped {
33743392
@Unwrapped.Nullable EmbeddableType embeddableValue;
33753393
}
33763394

3395+
static class WithUnwrappedConstructor {
3396+
3397+
private final String id;
3398+
3399+
private final @Unwrapped.Empty EmbeddableType embeddableValue;
3400+
3401+
public WithUnwrappedConstructor(String id, EmbeddableType embeddableValue) {
3402+
this.id = id;
3403+
this.embeddableValue = embeddableValue;
3404+
}
3405+
}
3406+
33773407
static class WithPrefixedNullableUnwrapped {
33783408

33793409
String id;

0 commit comments

Comments
 (0)