Skip to content

Commit 04e80ed

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

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
@@ -2346,6 +2346,24 @@ void readUnwrappedTypeWithComplexValue() {
23462346
.isEqualTo(expected);
23472347
}
23482348

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

@@ -3422,6 +3440,18 @@ static class WithNullableUnwrapped {
34223440
@Unwrapped.Nullable EmbeddableType embeddableValue;
34233441
}
34243442

3443+
static class WithUnwrappedConstructor {
3444+
3445+
private final String id;
3446+
3447+
private final @Unwrapped.Empty EmbeddableType embeddableValue;
3448+
3449+
public WithUnwrappedConstructor(String id, EmbeddableType embeddableValue) {
3450+
this.id = id;
3451+
this.embeddableValue = embeddableValue;
3452+
}
3453+
}
3454+
34253455
static class WithPrefixedNullableUnwrapped {
34263456

34273457
String id;

0 commit comments

Comments
 (0)