|
| 1 | +/* |
| 2 | + * Copyright 2020-2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.graphql.data.method.annotation.support; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +import jakarta.validation.Constraint; |
| 23 | +import jakarta.validation.ConstraintViolation; |
| 24 | +import jakarta.validation.ConstraintViolationException; |
| 25 | +import jakarta.validation.Valid; |
| 26 | +import jakarta.validation.Validator; |
| 27 | +import jakarta.validation.metadata.BeanDescriptor; |
| 28 | + |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.core.MethodParameter; |
| 31 | +import org.springframework.core.annotation.AnnotationUtils; |
| 32 | +import org.springframework.core.annotation.MergedAnnotations; |
| 33 | +import org.springframework.graphql.data.method.HandlerMethod; |
| 34 | +import org.springframework.lang.Nullable; |
| 35 | +import org.springframework.util.Assert; |
| 36 | +import org.springframework.validation.annotation.Validated; |
| 37 | +import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean; |
| 38 | +import org.springframework.validation.beanvalidation.SpringValidatorAdapter; |
| 39 | + |
| 40 | +/** |
| 41 | + * Helper class to apply standard bean validation to a {@link HandlerMethod}. |
| 42 | + * |
| 43 | + * @author Brian Clozel |
| 44 | + * @author Rossen Stoyanchev |
| 45 | + * @since 1.0 |
| 46 | + */ |
| 47 | +final class HandlerMethodValidationHelper { |
| 48 | + |
| 49 | + private final Validator validator; |
| 50 | + |
| 51 | + |
| 52 | + /** |
| 53 | + * Constructor with the {@link Validator} instance to use. |
| 54 | + */ |
| 55 | + public HandlerMethodValidationHelper(Validator validator) { |
| 56 | + Assert.notNull(validator, "validator should not be null"); |
| 57 | + if (validator instanceof LocalValidatorFactoryBean) { |
| 58 | + this.validator = ((LocalValidatorFactoryBean) validator).getValidator(); |
| 59 | + } |
| 60 | + else if (validator instanceof SpringValidatorAdapter) { |
| 61 | + this.validator = validator.unwrap(Validator.class); |
| 62 | + } |
| 63 | + else { |
| 64 | + this.validator = validator; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + /** |
| 70 | + * Validate the input values to a the {@link HandlerMethod} and throw a |
| 71 | + * {@link ConstraintViolationException} in case of violations. |
| 72 | + * @param handlerMethod the handler method to validate |
| 73 | + * @param arguments the input argument values |
| 74 | + */ |
| 75 | + public void validate(HandlerMethod handlerMethod, Object[] arguments) { |
| 76 | + Set<ConstraintViolation<Object>> result = |
| 77 | + this.validator.forExecutables().validateParameters( |
| 78 | + handlerMethod.getBean(), handlerMethod.getMethod(), arguments, |
| 79 | + determineValidationGroups(handlerMethod)); |
| 80 | + if (!result.isEmpty()) { |
| 81 | + throw new ConstraintViolationException(result); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Determine the validation groups to apply to a handler method, specified |
| 87 | + * through the {@link Validated} annotation on the method or on the class. |
| 88 | + * @param method the method to check |
| 89 | + * @return the applicable validation groups as a Class array |
| 90 | + */ |
| 91 | + private Class<?>[] determineValidationGroups(HandlerMethod method) { |
| 92 | + Validated annotation = findAnnotation(method, Validated.class); |
| 93 | + return (annotation != null ? annotation.value() : new Class<?>[0]); |
| 94 | + } |
| 95 | + |
| 96 | + @Nullable |
| 97 | + private static <A extends Annotation> A findAnnotation(HandlerMethod method, Class<A> annotationType) { |
| 98 | + A annotation = AnnotationUtils.findAnnotation(method.getMethod(), annotationType); |
| 99 | + if (annotation == null) { |
| 100 | + annotation = AnnotationUtils.findAnnotation(method.getBeanType(), annotationType); |
| 101 | + } |
| 102 | + return annotation; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Whether the given method requires standard bean validation. This is the |
| 107 | + * case if the method or one of its parameters are annotated with |
| 108 | + * {@link Valid} or {@link Validated}, or if any method parameter is declared |
| 109 | + * with a {@link Constraint constraint}, or the method parameter type is |
| 110 | + * itself {@link BeanDescriptor#isBeanConstrained() constrained}. |
| 111 | + * @param method the handler method to check |
| 112 | + * @return {@code true} if validation is required, {@code false} otherwise |
| 113 | + */ |
| 114 | + public boolean requiresValidation(HandlerMethod method) { |
| 115 | + if (findAnnotation(method, Validated.class) != null || findAnnotation(method, Valid.class) != null) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + for (MethodParameter parameter : method.getMethodParameters()) { |
| 119 | + for (Annotation annotation : parameter.getParameterAnnotations()) { |
| 120 | + MergedAnnotations merged = MergedAnnotations.from(annotation); |
| 121 | + if (merged.isPresent(Valid.class) || merged.isPresent(Constraint.class) || merged.isPresent(Validated.class)) { |
| 122 | + return true; |
| 123 | + } |
| 124 | + } |
| 125 | + Class<?> paramType = parameter.nestedIfOptional().getNestedParameterType(); |
| 126 | + if (this.validator.getConstraintsForClass(paramType).isBeanConstrained()) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + } |
| 130 | + return false; |
| 131 | + } |
| 132 | + |
| 133 | + |
| 134 | + /** |
| 135 | + * Factory method for {@link HandlerMethodValidationHelper} if a |
| 136 | + * {@link Validator} can be found. |
| 137 | + * @param context the context to look up a {@code Validator} bean from |
| 138 | + * @return the helper instance, or {@code null |
| 139 | + */ |
| 140 | + @Nullable |
| 141 | + public static HandlerMethodValidationHelper createIfValidatorAvailable(ApplicationContext context) { |
| 142 | + Validator validator = context.getBeanProvider(Validator.class).getIfAvailable(); |
| 143 | + return (validator != null ? new HandlerMethodValidationHelper(validator) : null); |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments