Skip to content

Commit b2809d6

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

File tree

2 files changed

+58
-4
lines changed

2 files changed

+58
-4
lines changed

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

+28-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@
1717

1818
import java.lang.reflect.Constructor;
1919
import java.lang.reflect.Method;
20-
import java.util.*;
20+
import java.util.ArrayList;
21+
import java.util.Arrays;
22+
import java.util.Collection;
23+
import java.util.Collections;
24+
import java.util.HashSet;
25+
import java.util.LinkedHashMap;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Optional;
29+
import java.util.Set;
2130
import java.util.function.Predicate;
2231
import java.util.stream.Collectors;
2332

@@ -41,7 +50,13 @@
4150
import org.springframework.data.annotation.Reference;
4251
import org.springframework.data.convert.CustomConversions;
4352
import org.springframework.data.convert.TypeMapper;
44-
import org.springframework.data.mapping.*;
53+
import org.springframework.data.mapping.Association;
54+
import org.springframework.data.mapping.InstanceCreatorMetadata;
55+
import org.springframework.data.mapping.MappingException;
56+
import org.springframework.data.mapping.Parameter;
57+
import org.springframework.data.mapping.PersistentEntity;
58+
import org.springframework.data.mapping.PersistentProperty;
59+
import org.springframework.data.mapping.PersistentPropertyAccessor;
4560
import org.springframework.data.mapping.callback.EntityCallbacks;
4661
import org.springframework.data.mapping.context.MappingContext;
4762
import org.springframework.data.mapping.model.ConvertingPropertyAccessor;
@@ -1961,6 +1976,8 @@ class AssociationAwareMongoDbPropertyValueProvider extends MongoDbPropertyValueP
19611976
@SuppressWarnings("unchecked")
19621977
public <T> T getPropertyValue(MongoPersistentProperty property) {
19631978

1979+
ConversionContext propertyContext = context.forProperty(property);
1980+
19641981
if (property.isDbReference() && property.getDBRef().lazy()) {
19651982

19661983
Object rawRefValue = accessor.get(property);
@@ -1977,9 +1994,16 @@ public <T> T getPropertyValue(MongoPersistentProperty property) {
19771994
}
19781995

19791996
if (property.isDocumentReference()) {
1997+
19801998
return (T) dbRefResolver.resolveReference(property,
1981-
new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)),
1982-
referenceLookupDelegate, context::convert);
1999+
new DocumentReferenceSource(accessor.getDocument(), accessor.get(property)), referenceLookupDelegate,
2000+
context::convert);
2001+
}
2002+
2003+
if (property.isUnwrapped()) {
2004+
2005+
return (T) readUnwrapped(propertyContext, accessor, property,
2006+
mappingContext.getRequiredPersistentEntity(property));
19832007
}
19842008

19852009
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)