Skip to content

Commit 146b352

Browse files
committed
Populate DTO projection properties that are considered associations.
We now populate DTO properties whose are identified as associations. While uncommon, as typically entities declare assocations using entities, associations can be identified as types and so these are now considered when populating properties. Closes #3104
1 parent d748e86 commit 146b352

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

Diff for: src/main/java/org/springframework/data/convert/DtoInstantiatingConverter.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.data.mapping.PersistentEntity;
2222
import org.springframework.data.mapping.PersistentProperty;
2323
import org.springframework.data.mapping.PersistentPropertyAccessor;
24+
import org.springframework.data.mapping.SimpleAssociationHandler;
2425
import org.springframework.data.mapping.SimplePropertyHandler;
2526
import org.springframework.data.mapping.context.MappingContext;
2627
import org.springframework.data.mapping.model.EntityInstantiator;
@@ -96,15 +97,19 @@ public Object getParameterValue(Parameter parameter) {
9697
PersistentPropertyAccessor<Object> targetAccessor = targetEntity.getPropertyAccessor(dto);
9798
InstanceCreatorMetadata<? extends PersistentProperty<?>> creator = targetEntity.getInstanceCreatorMetadata();
9899

99-
targetEntity.doWithProperties((SimplePropertyHandler) property -> {
100+
SimplePropertyHandler simplePropertyHandler = property -> {
100101

101102
if ((creator != null) && creator.isCreatorParameter(property)) {
102103
return;
103104
}
104105

105106
targetAccessor.setProperty(property,
106107
sourceAccessor.getProperty(sourceEntity.getRequiredPersistentProperty(property.getName())));
107-
});
108+
};
109+
110+
targetEntity.doWithProperties(simplePropertyHandler);
111+
targetEntity.doWithAssociations(
112+
(SimpleAssociationHandler) property -> simplePropertyHandler.doWithPersistentProperty(property.getInverse()));
108113

109114
return dto;
110115
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.convert;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import org.springframework.data.annotation.Reference;
23+
import org.springframework.data.mapping.context.SampleMappingContext;
24+
import org.springframework.data.mapping.model.EntityInstantiators;
25+
26+
/**
27+
* Unit tests for {@link DtoInstantiatingConverter}.
28+
*
29+
* @author Mark Paluch
30+
*/
31+
class DtoInstantiatingConverterUnitTests {
32+
33+
@Test // GH- 3104
34+
void dtoProjectionShouldConsiderPropertiesAndAssociations() {
35+
36+
TheOtherThing ref = new TheOtherThing();
37+
MyAssociativeEntity entity = new MyAssociativeEntity("1", ref, "foo");
38+
DtoInstantiatingConverter converter = new DtoInstantiatingConverter(DtoProjection.class, new SampleMappingContext(),
39+
new EntityInstantiators());
40+
41+
DtoProjection projection = (DtoProjection) converter.convert(entity);
42+
43+
assertThat(projection.id).isEqualTo("1");
44+
assertThat(projection.ref).isSameAs(ref);
45+
}
46+
47+
static class MyAssociativeEntity {
48+
49+
String id;
50+
51+
@Reference TheOtherThing ref;
52+
53+
String somethingElse;
54+
55+
public MyAssociativeEntity(String id, TheOtherThing ref, String somethingElse) {
56+
this.id = id;
57+
this.ref = ref;
58+
this.somethingElse = somethingElse;
59+
}
60+
}
61+
62+
static class DtoProjection {
63+
64+
String id;
65+
66+
@Reference TheOtherThing ref;
67+
}
68+
69+
static class TheOtherThing {
70+
String id;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)