Skip to content

Allow customizing order for AuditingEntityCallback and ValidatingEntityCallback #4968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
* {@link EntityCallback} to populate auditing related fields on an entity about to be saved.
*
* @author Mark Paluch
* @author yangchef1
* @since 2.2
*/
public class AuditingEntityCallback implements BeforeConvertCallback<Object>, Ordered {

private final ObjectFactory<IsNewAwareAuditingHandler> auditingHandlerFactory;
private int order = 100;

/**
* Creates a new {@link AuditingEntityCallback} using the given {@link MappingContext} and {@link AuditingHandler}
Expand All @@ -52,6 +54,10 @@ public Object onBeforeConvert(Object entity, String collection) {

@Override
public int getOrder() {
return 100;
return this.order;
}

public void setOrder(int order) {
this.order = order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@
*
* @author Rene Felgenträger
* @author Mark Paluch
* @author yangchef1
* @since 4.5
*/
public class ValidatingEntityCallback implements BeforeSaveCallback<Object>, Ordered {

private final BeanValidationDelegate delegate;
private int order = 100;

/**
* Creates a new {@link ValidatingEntityCallback} using the given {@link Validator}.
Expand All @@ -62,7 +64,10 @@ public Object onBeforeSave(Object entity, Document document, String collection)

@Override
public int getOrder() {
return 100;
return this.order;
}

public void setOrder(int order) {
this.order = order;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
* Unit tests for {@link AuditingEntityCallback}.
*
* @author Mark Paluch
* @author yangchef1
*/
@ExtendWith(MockitoExtension.class)
public class AuditingEntityCallbackUnitTests {
Expand Down Expand Up @@ -92,6 +93,16 @@ void hasExplicitOrder() {
assertThat(callback.getOrder()).isEqualTo(100);
}

@Test // GH-4914
void allowsChangingOrderDynamically() {

assertThat(callback).isInstanceOf(Ordered.class);
assertThat(callback.getOrder()).isEqualTo(100);

callback.setOrder(50);
assertThat(callback.getOrder()).isEqualTo(50);
}

@Test // DATAMONGO-2261
void propagatesChangedInstanceToEvent() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@
import org.bson.Document;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.core.Ordered;

/**
* Unit tests for {@link ValidatingEntityCallback}.
*
* @author Rene Felgenträger
* @author Mark Paluch
* @author yangchef1
*/
class ValidatingEntityCallbackUnitTests {

Expand Down Expand Up @@ -63,6 +65,16 @@ void validateSuccessful() {
assertThat(entity).isEqualTo(coordinates);
}

@Test // GH-4914
void allowsChangingOrderDynamically() {

assertThat(callback).isInstanceOf(Ordered.class);
assertThat(callback.getOrder()).isEqualTo(100);

callback.setOrder(50);
assertThat(callback.getOrder()).isEqualTo(50);
}

record Coordinates(@NotNull @Min(0) Integer x, @NotNull @Min(0) Integer y) {

Document toDocument() {
Expand Down