use of jakarta.validation.executable.ExecutableValidator in project spring-framework by spring-projects.
the class MethodValidationInterceptor method invoke.
@Override
@Nullable
public Object invoke(MethodInvocation invocation) throws Throwable {
// Avoid Validator invocation on FactoryBean.getObjectType/isSingleton
if (isFactoryBeanMetadataMethod(invocation.getMethod())) {
return invocation.proceed();
}
Class<?>[] groups = determineValidationGroups(invocation);
// Standard Bean Validation 1.1 API
ExecutableValidator execVal = this.validator.forExecutables();
Method methodToValidate = invocation.getMethod();
Set<ConstraintViolation<Object>> result;
Object target = invocation.getThis();
Assert.state(target != null, "Target must not be null");
try {
result = execVal.validateParameters(target, methodToValidate, invocation.getArguments(), groups);
} catch (IllegalArgumentException ex) {
// Probably a generic type mismatch between interface and impl as reported in SPR-12237 / HV-1011
// Let's try to find the bridged method on the implementation class...
methodToValidate = BridgeMethodResolver.findBridgedMethod(ClassUtils.getMostSpecificMethod(invocation.getMethod(), target.getClass()));
result = execVal.validateParameters(target, methodToValidate, invocation.getArguments(), groups);
}
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
Object returnValue = invocation.proceed();
result = execVal.validateReturnValue(target, methodToValidate, returnValue, groups);
if (!result.isEmpty()) {
throw new ConstraintViolationException(result);
}
return returnValue;
}
use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.
the class ConstraintValidatorContextTest method testAddParameterNodeUsingCustomParameterNameProvider.
@Test
@TestForIssue(jiraKey = "HV-709")
public void testAddParameterNodeUsingCustomParameterNameProvider() throws Exception {
ExecutableValidator executableValidator = ValidatorUtil.getConfiguration().parameterNameProvider(new PrefixableParameterNameProvider("param")).buildValidatorFactory().getValidator().forExecutables();
Set<ConstraintViolation<User>> constraintViolations = executableValidator.validateParameters(new User(), User.class.getMethod("setAddresses", Map.class), new java.lang.Object[] { new HashMap<>() });
assertThat(constraintViolations).containsOnlyPaths(pathWith().method("setAddresses").parameter("param0", 0), pathWith().method("setAddresses").parameter("param0", 0).bean(), pathWith().method("setAddresses").parameter("param0", 0).property("myNode1", true, null, 23).bean());
}
use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.
the class TypeVariableMethodParameterResolutionTest method testTypeVariableMethodParameterResolution.
@Test
public void testTypeVariableMethodParameterResolution() throws NoSuchMethodException, SecurityException {
ExecutableValidator validator = Validation.buildDefaultValidatorFactory().getValidator().forExecutables();
Set<ConstraintViolation<Bean>> violations = validator.validateParameters(new Bean(), Bean.class.getMethod("method", List.class), new Object[] { new ArrayList<>() });
assertThat(violations).containsOnlyViolations(violationOf(Size.class));
}
use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.
the class ConstraintViolationSerializationTest method testSuccessfulSerializationWithMethodReturnValue.
@Test
@TestForIssue(jiraKey = "HV-1485")
public void testSuccessfulSerializationWithMethodReturnValue() throws Exception {
ExecutableValidator validator = ValidatorUtil.getValidator().forExecutables();
SerializableClass testInstance = new SerializableClass("s");
Set<ConstraintViolation<SerializableClass>> constraintViolations = validator.validateReturnValue(testInstance, SerializableClass.class.getMethod("fooReturnValue"), "s");
byte[] bytes = serialize(constraintViolations);
Set<ConstraintViolation<?>> deserializedViolations = deserialize(bytes);
assertThat(deserializedViolations).containsOnlyViolations(violationOf(Size.class).withPropertyPath(pathWith().method("fooReturnValue").returnValue()));
checkSerializedViolation(deserializedViolations.iterator().next(), testInstance, null, "s");
}
use of jakarta.validation.executable.ExecutableValidator in project hibernate-validator by hibernate.
the class ConstraintViolationSerializationTest method testSuccessfulSerializationWithMethodParameters.
@Test
@TestForIssue(jiraKey = "HV-1485")
public void testSuccessfulSerializationWithMethodParameters() throws Exception {
ExecutableValidator validator = ValidatorUtil.getValidator().forExecutables();
SerializableClass testInstance = new SerializableClass("s");
Set<ConstraintViolation<SerializableClass>> constraintViolations = validator.validateParameters(testInstance, SerializableClass.class.getMethod("fooParameter", String.class), new Object[] { "s" });
byte[] bytes = serialize(constraintViolations);
Set<ConstraintViolation<?>> deserializedViolations = deserialize(bytes);
assertThat(deserializedViolations).containsOnlyViolations(violationOf(Size.class).withPropertyPath(pathWith().method("fooParameter").parameter("foo", 0)));
checkSerializedViolation(deserializedViolations.iterator().next(), testInstance, new Object[] { "s" }, null);
}
Aggregations