Browse Source
Introduce ReactiveValidatingEntityCallback, extract BeanValidationDelegate. Document Bean Validation callbacks. See #4901 Original pull request: #4910pull/4915/head
7 changed files with 302 additions and 57 deletions
@ -0,0 +1,72 @@ |
|||||||
|
/* |
||||||
|
* Copyright 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.Validator; |
||||||
|
|
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import org.apache.commons.logging.Log; |
||||||
|
import org.apache.commons.logging.LogFactory; |
||||||
|
|
||||||
|
import org.springframework.util.Assert; |
||||||
|
|
||||||
|
/** |
||||||
|
* Delegate to handle common calls to Bean {@link Validator Validation}. |
||||||
|
* |
||||||
|
* @author Mark Paluch |
||||||
|
* @since 4.5 |
||||||
|
*/ |
||||||
|
class BeanValidationDelegate { |
||||||
|
|
||||||
|
private static final Log LOG = LogFactory.getLog(BeanValidationDelegate.class); |
||||||
|
|
||||||
|
private final Validator validator; |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new {@link BeanValidationDelegate} using the given {@link Validator}. |
||||||
|
* |
||||||
|
* @param validator must not be {@literal null}. |
||||||
|
*/ |
||||||
|
public BeanValidationDelegate(Validator validator) { |
||||||
|
Assert.notNull(validator, "Validator must not be null"); |
||||||
|
this.validator = validator; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Validate the given object. |
||||||
|
* |
||||||
|
* @param object |
||||||
|
* @return set of constraint violations. |
||||||
|
*/ |
||||||
|
public Set<ConstraintViolation<Object>> validate(Object object) { |
||||||
|
|
||||||
|
if (LOG.isDebugEnabled()) { |
||||||
|
LOG.debug(String.format("Validating object: %s", object)); |
||||||
|
} |
||||||
|
|
||||||
|
Set<ConstraintViolation<Object>> violations = validator.validate(object); |
||||||
|
|
||||||
|
if (!violations.isEmpty()) { |
||||||
|
if (LOG.isDebugEnabled()) { |
||||||
|
LOG.info(String.format("During object: %s validation violations found: %s", object, violations)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return violations; |
||||||
|
} |
||||||
|
} |
||||||
@ -0,0 +1,69 @@ |
|||||||
|
/* |
||||||
|
* Copyright 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 reactor.core.publisher.Mono; |
||||||
|
|
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
import org.bson.Document; |
||||||
|
|
||||||
|
import org.springframework.core.Ordered; |
||||||
|
|
||||||
|
/** |
||||||
|
* Reactive variant of JSR-303 dependant entities validator. |
||||||
|
* <p> |
||||||
|
* When it is registered as Spring component its automatically invoked after object to {@link Document} conversion and |
||||||
|
* before entities are saved to the database. |
||||||
|
* |
||||||
|
* @author Mark Paluch |
||||||
|
* @author Rene Felgenträger |
||||||
|
* @since 4.5 |
||||||
|
*/ |
||||||
|
public class ReactiveValidatingEntityCallback implements ReactiveBeforeSaveCallback<Object>, Ordered { |
||||||
|
|
||||||
|
private final BeanValidationDelegate delegate; |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a new {@link ReactiveValidatingEntityCallback} using the given {@link Validator}. |
||||||
|
* |
||||||
|
* @param validator must not be {@literal null}. |
||||||
|
*/ |
||||||
|
public ReactiveValidatingEntityCallback(Validator validator) { |
||||||
|
this.delegate = new BeanValidationDelegate(validator); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public Mono<Object> onBeforeSave(Object entity, Document document, String collection) { |
||||||
|
|
||||||
|
Set<ConstraintViolation<Object>> violations = delegate.validate(entity); |
||||||
|
|
||||||
|
if (!violations.isEmpty()) { |
||||||
|
return Mono.error(new ConstraintViolationException(violations)); |
||||||
|
} |
||||||
|
|
||||||
|
return Mono.just(entity); |
||||||
|
} |
||||||
|
|
||||||
|
@Override |
||||||
|
public int getOrder() { |
||||||
|
return 100; |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
@ -0,0 +1,79 @@ |
|||||||
|
/* |
||||||
|
* Copyright 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.ConstraintViolationException; |
||||||
|
import jakarta.validation.Validation; |
||||||
|
import jakarta.validation.ValidatorFactory; |
||||||
|
import jakarta.validation.constraints.Min; |
||||||
|
import jakarta.validation.constraints.NotNull; |
||||||
|
import reactor.test.StepVerifier; |
||||||
|
|
||||||
|
import org.bson.Document; |
||||||
|
import org.junit.jupiter.api.BeforeEach; |
||||||
|
import org.junit.jupiter.api.Test; |
||||||
|
|
||||||
|
/** |
||||||
|
* Unit tests for {@link ReactiveValidatingEntityCallback}. |
||||||
|
* |
||||||
|
* @author Mark Paluch |
||||||
|
* @author Rene Felgenträger |
||||||
|
*/ |
||||||
|
class ReactiveValidatingEntityCallbackUnitTests { |
||||||
|
|
||||||
|
private ReactiveValidatingEntityCallback callback; |
||||||
|
|
||||||
|
@BeforeEach |
||||||
|
void setUp() { |
||||||
|
try (ValidatorFactory factory = Validation.buildDefaultValidatorFactory()) { |
||||||
|
callback = new ReactiveValidatingEntityCallback(factory.getValidator()); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
@Test // GH-4910
|
||||||
|
void validationThrowsException() { |
||||||
|
|
||||||
|
Coordinates coordinates = new Coordinates(-1, -1); |
||||||
|
|
||||||
|
callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates") //
|
||||||
|
.as(StepVerifier::create) //
|
||||||
|
.verifyError(ConstraintViolationException.class); |
||||||
|
} |
||||||
|
|
||||||
|
@Test // GH-4910
|
||||||
|
void validateSuccessful() { |
||||||
|
|
||||||
|
Coordinates coordinates = new Coordinates(0, 0); |
||||||
|
|
||||||
|
callback.onBeforeSave(coordinates, coordinates.toDocument(), "coordinates") //
|
||||||
|
.as(StepVerifier::create) //
|
||||||
|
.expectNext(coordinates) //
|
||||||
|
.verifyComplete(); |
||||||
|
} |
||||||
|
|
||||||
|
record Coordinates(@NotNull @Min(0) Integer x, @NotNull @Min(0) Integer y) { |
||||||
|
|
||||||
|
Document toDocument() { |
||||||
|
return Document.parse(""" |
||||||
|
{ |
||||||
|
"x": %d, |
||||||
|
"y": %d |
||||||
|
} |
||||||
|
""".formatted(x, y)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
Loading…
Reference in new issue