Skip to content

Commit 1545e18

Browse files
committed
Apply unwrapped persistent property equality check to delegate.
We now compare the other object whether it equals the delegate in case UnwrappedMongoPersistentProperty.equals is being called with the MongoPersistentProperty retrieved from a MappingContext. This ensures that unwrapped properties can be compared to vanilla MongoPersistentProperty instances when checking constructor/creator method correlation of parameters. Closes #4732
1 parent 4c04aab commit 1545e18

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapping/UnwrappedMongoPersistentProperty.java

+4
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,10 @@ public boolean equals(@Nullable Object obj) {
362362
return true;
363363
}
364364

365+
if (obj == delegate) {
366+
return true;
367+
}
368+
365369
if (obj == null || getClass() != obj.getClass()) {
366370
return false;
367371
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/mapping/event/AuditingEntityCallbackUnitTests.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
3535
import org.springframework.data.mapping.context.PersistentEntities;
3636
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
37+
import org.springframework.data.mongodb.core.mapping.Unwrapped;
3738

3839
/**
3940
* Unit tests for {@link AuditingEntityCallback}.
@@ -43,13 +44,14 @@
4344
@ExtendWith(MockitoExtension.class)
4445
public class AuditingEntityCallbackUnitTests {
4546

47+
private final MongoMappingContext mappingContext = new MongoMappingContext();
48+
4649
private IsNewAwareAuditingHandler handler;
4750
private AuditingEntityCallback callback;
4851

4952
@BeforeEach
5053
void setUp() {
5154

52-
MongoMappingContext mappingContext = new MongoMappingContext();
5355
mappingContext.getPersistentEntity(Sample.class);
5456

5557
handler = spy(new IsNewAwareAuditingHandler(new PersistentEntities(Arrays.asList(mappingContext))));
@@ -105,13 +107,40 @@ void propagatesChangedInstanceToEvent() {
105107
assertThat(result).isSameAs(newSample);
106108
}
107109

110+
@Test // GH-4732
111+
void shouldApplyAuditingToUnwrappedImmutableObject() {
112+
113+
WithUnwrapped sample = new WithUnwrapped();
114+
sample.auditingData = new MyAuditingData(null, null);
115+
116+
IsNewAwareAuditingHandler handler = new IsNewAwareAuditingHandler(PersistentEntities.of(mappingContext));
117+
118+
AuditingEntityCallback listener = new AuditingEntityCallback(() -> handler);
119+
WithUnwrapped result = (WithUnwrapped) listener.onBeforeConvert(sample, "foo");
120+
121+
assertThat(result.auditingData.created).isNotNull();
122+
assertThat(result.auditingData.modified).isNotNull();
123+
}
124+
108125
static class Sample {
109126

110127
@Id String id;
111128
@CreatedDate Date created;
112129
@LastModifiedDate Date modified;
113130
}
114131

132+
static class WithUnwrapped {
133+
134+
@Id String id;
135+
136+
@Unwrapped(onEmpty = Unwrapped.OnEmpty.USE_NULL) MyAuditingData auditingData;
137+
138+
}
139+
140+
record MyAuditingData(@CreatedDate Date created, @LastModifiedDate Date modified) {
141+
142+
}
143+
115144
private static final class ImmutableSample {
116145

117146
@Id private final String id;

0 commit comments

Comments
 (0)