Skip to content

Commit

Permalink
Apply unwrapped persistent property equality check to delegate.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mp911de committed Jun 27, 2024
1 parent 2ef3ba0 commit 9a24c6c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,10 @@ public boolean equals(@Nullable Object obj) {
return true;
}

if (obj == delegate) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.springframework.data.auditing.IsNewAwareAuditingHandler;
import org.springframework.data.mapping.context.PersistentEntities;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.core.mapping.Unwrapped;

/**
* Unit tests for {@link AuditingEntityCallback}.
Expand All @@ -43,13 +44,14 @@
@ExtendWith(MockitoExtension.class)
public class AuditingEntityCallbackUnitTests {

private final MongoMappingContext mappingContext = new MongoMappingContext();

private IsNewAwareAuditingHandler handler;
private AuditingEntityCallback callback;

@BeforeEach
void setUp() {

MongoMappingContext mappingContext = new MongoMappingContext();
mappingContext.getPersistentEntity(Sample.class);

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

@Test // GH-4732
void shouldApplyAuditingToUnwrappedImmutableObject() {

WithUnwrapped sample = new WithUnwrapped();
sample.auditingData = new MyAuditingData(null, null);

IsNewAwareAuditingHandler handler = new IsNewAwareAuditingHandler(PersistentEntities.of(mappingContext));

AuditingEntityCallback listener = new AuditingEntityCallback(() -> handler);
WithUnwrapped result = (WithUnwrapped) listener.onBeforeConvert(sample, "foo");

assertThat(result.auditingData.created).isNotNull();
assertThat(result.auditingData.modified).isNotNull();
}

static class Sample {

@Id String id;
@CreatedDate Date created;
@LastModifiedDate Date modified;
}

static class WithUnwrapped {

@Id String id;

@Unwrapped(onEmpty = Unwrapped.OnEmpty.USE_NULL) MyAuditingData auditingData;

}

record MyAuditingData(@CreatedDate Date created, @LastModifiedDate Date modified) {

}

private static final class ImmutableSample {

@Id private final String id;
Expand Down

0 comments on commit 9a24c6c

Please sign in to comment.