Use SearchStrategy in MethodValidationExcludeFilter byAnnotation

Closes gh-30631
pull/30878/head
Andy Wilkinson 3 years ago
parent 9b8b87009c
commit ffe0ec6d4d

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2021 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -40,7 +40,8 @@ public interface MethodValidationExcludeFilter {
/** /**
* Factory method to create a {@link MethodValidationExcludeFilter} that excludes * Factory method to create a {@link MethodValidationExcludeFilter} that excludes
* classes by annotation. * classes by annotation found using an {@link SearchStrategy#INHERITED_ANNOTATIONS
* inherited annotations search strategy}.
* @param annotationType the annotation to check * @param annotationType the annotation to check
* @return a {@link MethodValidationExcludeFilter} instance * @return a {@link MethodValidationExcludeFilter} instance
*/ */
@ -50,14 +51,14 @@ public interface MethodValidationExcludeFilter {
/** /**
* Factory method to create a {@link MethodValidationExcludeFilter} that excludes * Factory method to create a {@link MethodValidationExcludeFilter} that excludes
* classes by annotation. * classes by annotation found using the given search strategy.
* @param annotationType the annotation to check * @param annotationType the annotation to check
* @param searchStrategy the annotation search strategy * @param searchStrategy the annotation search strategy
* @return a {@link MethodValidationExcludeFilter} instance * @return a {@link MethodValidationExcludeFilter} instance
*/ */
static MethodValidationExcludeFilter byAnnotation(Class<? extends Annotation> annotationType, static MethodValidationExcludeFilter byAnnotation(Class<? extends Annotation> annotationType,
SearchStrategy searchStrategy) { SearchStrategy searchStrategy) {
return (type) -> MergedAnnotations.from(type, SearchStrategy.SUPERCLASS).isPresent(annotationType); return (type) -> MergedAnnotations.from(type, searchStrategy).isPresent(annotationType);
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2020 the original author or authors. * Copyright 2012-2022 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -16,11 +16,14 @@
package org.springframework.boot.validation.beanvalidation; package org.springframework.boot.validation.beanvalidation;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy; import java.lang.annotation.RetentionPolicy;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
/** /**
@ -42,6 +45,19 @@ class MethodValidationExcludeFilterTests {
assertThat(filter.isExcluded(Plain.class)).isFalse(); assertThat(filter.isExcluded(Plain.class)).isFalse();
} }
@Test
void byAnnotationWhenSuperclassIsAnnotatedWithInheritedAnnotationExcludes() {
MethodValidationExcludeFilter filter = MethodValidationExcludeFilter.byAnnotation(Indicator.class);
assertThat(filter.isExcluded(AnnotatedSuperClass.class)).isTrue();
}
@Test
void byAnnotationWithDirectSearchStrategyWhenSuperclassIsAnnotatedWithInheritedAnnotationIncludes() {
MethodValidationExcludeFilter filter = MethodValidationExcludeFilter.byAnnotation(Indicator.class,
SearchStrategy.DIRECT);
assertThat(filter.isExcluded(AnnotatedSuperClass.class)).isFalse();
}
static class Plain { static class Plain {
} }
@ -51,9 +67,14 @@ class MethodValidationExcludeFilterTests {
} }
@Inherited
@Retention(RetentionPolicy.RUNTIME) @Retention(RetentionPolicy.RUNTIME)
@interface Indicator { @interface Indicator {
} }
static class AnnotatedSuperClass extends Annotated {
}
} }

Loading…
Cancel
Save