-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Introduce ValidatingEntityCallback
and deprecate ValidatingMongoEventListener
#4910
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
364e258
gh-4901: introduce ValidatingEntityCallback
rfelgent e901ed3
gh-4901: introduce ValidatingEntityCallback
rfelgent 3cde56b
gh-4901: introduce ValidatingEntityCallback
rfelgent 2e1bdcd
gh-4901: introduce ValidatingEntityCallback
rfelgent c0e7bfb
gh-4901: introduce ValidatingEntityCallback
rfelgent 51538dd
gh-4901: introduce ValidatingEntityCallback
rfelgent File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
...in/java/org/springframework/data/mongodb/core/mapping/event/ValidatingEntityCallback.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.core.mapping.event; | ||
|
||
import jakarta.validation.ConstraintViolation; | ||
import jakarta.validation.ConstraintViolationException; | ||
import jakarta.validation.Validator; | ||
import java.util.Set; | ||
import org.apache.commons.logging.Log; | ||
import org.apache.commons.logging.LogFactory; | ||
import org.bson.Document; | ||
import org.springframework.core.Ordered; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* JSR-303 dependant entities validator. | ||
* <p> | ||
* When it is registered as Spring component its automatically invoked after any {@link AbstractMongoEventListener} and | ||
* before entities are saved in database. | ||
* | ||
* @author original authors of {@link ValidatingMongoEventListener} | ||
* @author Rene Felgenträger | ||
* @see {@link ValidatingMongoEventListener} | ||
*/ | ||
public class ValidatingEntityCallback implements BeforeSaveCallback<Object>, Ordered { | ||
|
||
private static final Log LOG = LogFactory.getLog(ValidatingEntityCallback.class); | ||
|
||
// TODO: create a validation handler (similar to "AuditingHandler") an reference it from "ValidatingMongoEventListener" and "ValidatingMongoEventListener" | ||
private final Validator validator; | ||
|
||
/** | ||
* Creates a new {@link ValidatingEntityCallback} using the given {@link Validator}. | ||
* | ||
* @param validator must not be {@literal null}. | ||
*/ | ||
public ValidatingEntityCallback(Validator validator) { | ||
Assert.notNull(validator, "Validator must not be null"); | ||
this.validator = validator; | ||
} | ||
|
||
// TODO: alternatively implement the "BeforeConvertCallback" interface and set the order to highest value ? | ||
@Override | ||
public Object onBeforeSave(Object entity, Document document, String collection) { | ||
|
||
if (LOG.isDebugEnabled()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pls tell me, is "debug" or "trace" the appropriate choice for bean validation ? |
||
LOG.debug(String.format("Validating object: %s", entity)); | ||
} | ||
Set<ConstraintViolation<Object>> violations = validator.validate(entity); | ||
|
||
if (!violations.isEmpty()) { | ||
if (LOG.isDebugEnabled()) { | ||
LOG.info(String.format("During object: %s validation violations found: %s", entity, violations)); | ||
} | ||
throw new ConstraintViolationException(violations); | ||
} | ||
return entity; | ||
} | ||
|
||
@Override | ||
public int getOrder() { | ||
return 100; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
...rg/springframework/data/mongodb/core/mapping/event/ValidatingEntityCallbackUnitTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2012-2025 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.mongodb.core.mapping.event; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
|
||
import jakarta.validation.ConstraintViolationException; | ||
import jakarta.validation.Validation; | ||
import jakarta.validation.ValidatorFactory; | ||
import jakarta.validation.constraints.Min; | ||
import jakarta.validation.constraints.NotNull; | ||
import org.bson.Document; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* Unit test for {@link ValidatingEntityCallback}. | ||
* | ||
* @author Rene Felgenträger | ||
*/ | ||
class ValidatingEntityCallbackUnitTests { | ||
|
||
private ValidatingEntityCallback callback; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { | ||
callback = new ValidatingEntityCallback(factory.getValidator()); | ||
} | ||
} | ||
|
||
@Test | ||
// GH-4910 | ||
void invalidModel_throwsException() { | ||
Coordinates coordinates = new Coordinates(-1, -1); | ||
|
||
assertThatExceptionOfType(ConstraintViolationException.class).isThrownBy( | ||
() -> callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates")) | ||
.satisfies(e -> assertThat(e.getConstraintViolations()).hasSize(2)); | ||
} | ||
|
||
@Test | ||
// GH-4910 | ||
void validModel_noExceptionThrown() { | ||
Coordinates coordinates = new Coordinates(0, 0); | ||
Object entity = callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates"); | ||
assertThat(entity).isEqualTo(coordinates); | ||
} | ||
|
||
record Coordinates(@NotNull @Min(0) Integer x, @NotNull @Min(0) Integer y) { | ||
|
||
Document toDocument() { | ||
return Document.parse(""" | ||
{ | ||
"x": %d, | ||
"y": %d | ||
} | ||
""".formatted(x, y)); | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
personally, I could live with this implementation (and duplicate the validation code logic of
ValidatingMongoEventListener
)