Search in sources :

Example 1 with SimpleValueReceiver

use of io.micronaut.validation.validator.extractors.SimpleValueReceiver in project micronaut-core by micronaut-projects.

the class DefaultValidator method validatePropertyInternal.

private <T> void validatePropertyInternal(@Nullable Class<T> rootBeanClass, @Nullable T rootBean, @Nullable Object object, @NonNull DefaultConstraintValidatorContext context, @NonNull Set<ConstraintViolation<Object>> overallViolations, @NonNull Class propertyType, @NonNull AnnotatedElement constrainedProperty, @Nullable Object propertyValue) {
    final AnnotationMetadata annotationMetadata = constrainedProperty.getAnnotationMetadata();
    final List<Class<? extends Annotation>> constraintTypes = annotationMetadata.getAnnotationTypesByStereotype(Constraint.class);
    for (Class<? extends Annotation> constraintType : constraintTypes) {
        ValueExtractor<Object> valueExtractor = null;
        if (propertyValue != null && !annotationMetadata.hasAnnotation(Valid.class)) {
            // noinspection unchecked
            valueExtractor = valueExtractorRegistry.findUnwrapValueExtractor((Class<Object>) propertyValue.getClass()).orElse(null);
        }
        if (valueExtractor != null) {
            valueExtractor.extractValues(propertyValue, (SimpleValueReceiver) (nodeName, extractedValue) -> valueConstraintOnProperty(rootBeanClass, rootBean, object, context, overallViolations, constrainedProperty, propertyType, extractedValue, constraintType));
        } else {
            valueConstraintOnProperty(rootBeanClass, rootBean, object, context, overallViolations, constrainedProperty, propertyType, propertyValue, constraintType);
        }
    }
}
Also used : Publishers(io.micronaut.core.async.publisher.Publishers) ArrayUtils(io.micronaut.core.util.ArrayUtils) ConstraintValidatorRegistry(io.micronaut.validation.validator.constraints.ConstraintValidatorRegistry) MessageSource(io.micronaut.context.MessageSource) ElementDescriptor(javax.validation.metadata.ElementDescriptor) Valid(javax.validation.Valid) ValueExtractor(javax.validation.valueextraction.ValueExtractor) BeanResolutionContext(io.micronaut.context.BeanResolutionContext) Path(javax.validation.Path) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) ConstraintViolation(javax.validation.ConstraintViolation) Method(java.lang.reflect.Method) Property(io.micronaut.context.annotation.Property) ClassUtils(io.micronaut.core.reflect.ClassUtils) MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue) MethodReference(io.micronaut.inject.MethodReference) Singleton(jakarta.inject.Singleton) ConstraintValidatorContext(io.micronaut.validation.validator.constraints.ConstraintValidatorContext) Collectors(java.util.stream.Collectors) Introspected(io.micronaut.core.annotation.Introspected) StringUtils(io.micronaut.core.util.StringUtils) CompletionStage(java.util.concurrent.CompletionStage) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) Scope(javax.validation.metadata.Scope) BeanDescriptor(javax.validation.metadata.BeanDescriptor) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) InjectionPoint(io.micronaut.inject.InjectionPoint) ArgumentValue(io.micronaut.core.type.ArgumentValue) ConstraintDescriptor(javax.validation.metadata.ConstraintDescriptor) ClockProvider(javax.validation.ClockProvider) java.util(java.util) BeanProperty(io.micronaut.core.beans.BeanProperty) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) ExecutableMethod(io.micronaut.inject.ExecutableMethod) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) SimpleValueReceiver(io.micronaut.validation.validator.extractors.SimpleValueReceiver) MethodType(javax.validation.metadata.MethodType) MethodDescriptor(javax.validation.metadata.MethodDescriptor) Primary(io.micronaut.context.annotation.Primary) ConstraintValidator(io.micronaut.validation.validator.constraints.ConstraintValidator) Nullable(io.micronaut.core.annotation.Nullable) Requires(io.micronaut.context.annotation.Requires) ReturnType(io.micronaut.core.type.ReturnType) ConfigurationReader(io.micronaut.context.annotation.ConfigurationReader) Argument(io.micronaut.core.type.Argument) AnnotatedElement(io.micronaut.core.annotation.AnnotatedElement) ValueExtractorRegistry(io.micronaut.validation.validator.extractors.ValueExtractorRegistry) Default(javax.validation.groups.Default) Publisher(org.reactivestreams.Publisher) BeanDefinitionValidator(io.micronaut.inject.validation.BeanDefinitionValidator) ConstructorDescriptor(javax.validation.metadata.ConstructorDescriptor) ElementType(java.lang.annotation.ElementType) TraversableResolver(javax.validation.TraversableResolver) NonNull(io.micronaut.core.annotation.NonNull) Flux(reactor.core.publisher.Flux) ExecutionHandleLocator(io.micronaut.context.ExecutionHandleLocator) ConstraintViolationException(javax.validation.ConstraintViolationException) Constraint(javax.validation.Constraint) ValidationException(javax.validation.ValidationException) CollectionUtils(io.micronaut.core.util.CollectionUtils) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) BeanDefinition(io.micronaut.inject.BeanDefinition) ElementKind(javax.validation.ElementKind) AnnotatedElementValidator(io.micronaut.inject.annotation.AnnotatedElementValidator) Intercepted(io.micronaut.aop.Intercepted) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Annotation(java.lang.annotation.Annotation)

Example 2 with SimpleValueReceiver

use of io.micronaut.validation.validator.extractors.SimpleValueReceiver in project micronaut-core by micronaut-projects.

the class DefaultValidator method validateParametersInternal.

private <T> void validateParametersInternal(@NonNull Class<T> rootClass, @Nullable T object, @NonNull Object[] parameters, Argument[] arguments, int argLen, DefaultConstraintValidatorContext context, Set overallViolations, Path.Node parentNode) {
    for (int i = 0; i < argLen; i++) {
        Argument argument = arguments[i];
        final Class<?> parameterType = argument.getType();
        final AnnotationMetadata annotationMetadata = argument.getAnnotationMetadata();
        final boolean hasValid = annotationMetadata.hasStereotype(Validator.ANN_VALID);
        final boolean hasConstraint = annotationMetadata.hasStereotype(Validator.ANN_CONSTRAINT);
        if (!hasValid && !hasConstraint) {
            continue;
        }
        Object parameterValue = parameters[i];
        ValueExtractor<Object> valueExtractor = null;
        final boolean hasValue = parameterValue != null;
        final boolean isValid = hasValue && hasValid;
        final boolean isPublisher = hasValue && Publishers.isConvertibleToPublisher(parameterType);
        if (isPublisher) {
            instrumentPublisherArgumentWithValidation(rootClass, object, parameters, context, i, argument, parameterType, annotationMetadata, parameterValue, isValid);
        } else {
            final boolean isCompletionStage = hasValue && CompletionStage.class.isAssignableFrom(parameterType);
            if (isCompletionStage) {
                instrumentCompletionStageArgumentWithValidation(rootClass, object, parameters, context, i, argument, annotationMetadata, parameterValue, isValid);
            } else {
                if (hasValue) {
                    // noinspection unchecked
                    valueExtractor = (ValueExtractor<Object>) valueExtractorRegistry.findUnwrapValueExtractor(parameterType).orElse(null);
                }
                int finalIndex = i;
                if (valueExtractor != null) {
                    valueExtractor.extractValues(parameterValue, (SimpleValueReceiver) (nodeName, unwrappedValue) -> validateParameterInternal(rootClass, object, parameters, context, overallViolations, argument.getName(), unwrappedValue == null ? Object.class : unwrappedValue.getClass(), finalIndex, annotationMetadata, unwrappedValue));
                } else {
                    validateParameterInternal(rootClass, object, parameters, context, overallViolations, argument.getName(), parameterType, finalIndex, annotationMetadata, parameterValue);
                }
                if (isValid) {
                    if (context.validatedObjects.contains(parameterValue)) {
                        // already validated
                        continue;
                    }
                    // cascade to bean
                    // noinspection unchecked
                    valueExtractor = (ValueExtractor<Object>) valueExtractorRegistry.findValueExtractor(parameterType).orElse(null);
                    if (valueExtractor != null) {
                        valueExtractor.extractValues(parameterValue, new ValueExtractor.ValueReceiver() {

                            @Override
                            public void value(String nodeName, Object object1) {
                            }

                            @Override
                            public void iterableValue(String nodeName, Object iterableValue) {
                                if (iterableValue != null && context.validatedObjects.contains(iterableValue)) {
                                    return;
                                }
                                cascadeToIterableValue(context, rootClass, object, parameterValue, parentNode, argument, iterableValue, overallViolations, null, null, true);
                            }

                            @Override
                            public void indexedValue(String nodeName, int i, Object iterableValue) {
                                if (iterableValue != null && context.validatedObjects.contains(iterableValue)) {
                                    return;
                                }
                                cascadeToIterableValue(context, rootClass, object, parameterValue, parentNode, argument, iterableValue, overallViolations, i, null, true);
                            }

                            @Override
                            public void keyedValue(String nodeName, Object key, Object keyedValue) {
                                if (keyedValue != null && context.validatedObjects.contains(keyedValue)) {
                                    return;
                                }
                                cascadeToIterableValue(context, rootClass, object, parameterValue, parentNode, argument, keyedValue, overallViolations, null, key, false);
                            }
                        });
                    } else {
                        final BeanIntrospection<Object> beanIntrospection = getBeanIntrospection(parameterValue, parameterType);
                        if (beanIntrospection != null) {
                            try {
                                context.addParameterNode(argument.getName(), i);
                                cascadeToOneIntrospection(context, object, parameterValue, beanIntrospection, overallViolations);
                            } finally {
                                context.removeLast();
                            }
                        } else {
                            context.addParameterNode(argument.getName(), i);
                            overallViolations.add(createIntrospectionConstraintViolation(rootClass, object, context, parameterType, parameterValue, parameters));
                            context.removeLast();
                        }
                    }
                }
            }
        }
    }
}
Also used : Publishers(io.micronaut.core.async.publisher.Publishers) ArrayUtils(io.micronaut.core.util.ArrayUtils) ConstraintValidatorRegistry(io.micronaut.validation.validator.constraints.ConstraintValidatorRegistry) MessageSource(io.micronaut.context.MessageSource) ElementDescriptor(javax.validation.metadata.ElementDescriptor) Valid(javax.validation.Valid) ValueExtractor(javax.validation.valueextraction.ValueExtractor) BeanResolutionContext(io.micronaut.context.BeanResolutionContext) Path(javax.validation.Path) PropertyDescriptor(javax.validation.metadata.PropertyDescriptor) ArgumentUtils(io.micronaut.core.util.ArgumentUtils) ConstraintViolation(javax.validation.ConstraintViolation) Method(java.lang.reflect.Method) Property(io.micronaut.context.annotation.Property) ClassUtils(io.micronaut.core.reflect.ClassUtils) MutableArgumentValue(io.micronaut.core.type.MutableArgumentValue) MethodReference(io.micronaut.inject.MethodReference) Singleton(jakarta.inject.Singleton) ConstraintValidatorContext(io.micronaut.validation.validator.constraints.ConstraintValidatorContext) Collectors(java.util.stream.Collectors) Introspected(io.micronaut.core.annotation.Introspected) StringUtils(io.micronaut.core.util.StringUtils) CompletionStage(java.util.concurrent.CompletionStage) AnnotationValue(io.micronaut.core.annotation.AnnotationValue) Annotation(java.lang.annotation.Annotation) Scope(javax.validation.metadata.Scope) BeanDescriptor(javax.validation.metadata.BeanDescriptor) BeanIntrospector(io.micronaut.core.beans.BeanIntrospector) InjectionPoint(io.micronaut.inject.InjectionPoint) ArgumentValue(io.micronaut.core.type.ArgumentValue) ConstraintDescriptor(javax.validation.metadata.ConstraintDescriptor) ClockProvider(javax.validation.ClockProvider) java.util(java.util) BeanProperty(io.micronaut.core.beans.BeanProperty) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) ExecutableMethod(io.micronaut.inject.ExecutableMethod) BeanInstantiationException(io.micronaut.context.exceptions.BeanInstantiationException) SimpleValueReceiver(io.micronaut.validation.validator.extractors.SimpleValueReceiver) MethodType(javax.validation.metadata.MethodType) MethodDescriptor(javax.validation.metadata.MethodDescriptor) Primary(io.micronaut.context.annotation.Primary) ConstraintValidator(io.micronaut.validation.validator.constraints.ConstraintValidator) Nullable(io.micronaut.core.annotation.Nullable) Requires(io.micronaut.context.annotation.Requires) ReturnType(io.micronaut.core.type.ReturnType) ConfigurationReader(io.micronaut.context.annotation.ConfigurationReader) Argument(io.micronaut.core.type.Argument) AnnotatedElement(io.micronaut.core.annotation.AnnotatedElement) ValueExtractorRegistry(io.micronaut.validation.validator.extractors.ValueExtractorRegistry) Default(javax.validation.groups.Default) Publisher(org.reactivestreams.Publisher) BeanDefinitionValidator(io.micronaut.inject.validation.BeanDefinitionValidator) ConstructorDescriptor(javax.validation.metadata.ConstructorDescriptor) ElementType(java.lang.annotation.ElementType) TraversableResolver(javax.validation.TraversableResolver) NonNull(io.micronaut.core.annotation.NonNull) Flux(reactor.core.publisher.Flux) ExecutionHandleLocator(io.micronaut.context.ExecutionHandleLocator) ConstraintViolationException(javax.validation.ConstraintViolationException) Constraint(javax.validation.Constraint) ValidationException(javax.validation.ValidationException) CollectionUtils(io.micronaut.core.util.CollectionUtils) BeanIntrospection(io.micronaut.core.beans.BeanIntrospection) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) BeanDefinition(io.micronaut.inject.BeanDefinition) ElementKind(javax.validation.ElementKind) AnnotatedElementValidator(io.micronaut.inject.annotation.AnnotatedElementValidator) Intercepted(io.micronaut.aop.Intercepted) Argument(io.micronaut.core.type.Argument) ValueExtractor(javax.validation.valueextraction.ValueExtractor) InjectionPoint(io.micronaut.inject.InjectionPoint) Constraint(javax.validation.Constraint) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) CompletionStage(java.util.concurrent.CompletionStage)

Aggregations

Intercepted (io.micronaut.aop.Intercepted)2 BeanResolutionContext (io.micronaut.context.BeanResolutionContext)2 ExecutionHandleLocator (io.micronaut.context.ExecutionHandleLocator)2 MessageSource (io.micronaut.context.MessageSource)2 ConfigurationReader (io.micronaut.context.annotation.ConfigurationReader)2 Primary (io.micronaut.context.annotation.Primary)2 Property (io.micronaut.context.annotation.Property)2 Requires (io.micronaut.context.annotation.Requires)2 BeanInstantiationException (io.micronaut.context.exceptions.BeanInstantiationException)2 AnnotatedElement (io.micronaut.core.annotation.AnnotatedElement)2 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 AnnotationValue (io.micronaut.core.annotation.AnnotationValue)2 Introspected (io.micronaut.core.annotation.Introspected)2 NonNull (io.micronaut.core.annotation.NonNull)2 Nullable (io.micronaut.core.annotation.Nullable)2 Publishers (io.micronaut.core.async.publisher.Publishers)2 BeanIntrospection (io.micronaut.core.beans.BeanIntrospection)2 BeanIntrospector (io.micronaut.core.beans.BeanIntrospector)2 BeanProperty (io.micronaut.core.beans.BeanProperty)2 ClassUtils (io.micronaut.core.reflect.ClassUtils)2