diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilter.java index d3d5b43f7332..888183b0f746 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilter.java @@ -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"); * 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 - * classes by annotation. + * classes by annotation found using an {@link SearchStrategy#INHERITED_ANNOTATIONS + * inherited annotations search strategy}. * @param annotationType the annotation to check * @return a {@link MethodValidationExcludeFilter} instance */ @@ -50,14 +51,14 @@ static MethodValidationExcludeFilter byAnnotation(Class an /** * 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 searchStrategy the annotation search strategy * @return a {@link MethodValidationExcludeFilter} instance */ static MethodValidationExcludeFilter byAnnotation(Class annotationType, SearchStrategy searchStrategy) { - return (type) -> MergedAnnotations.from(type, SearchStrategy.SUPERCLASS).isPresent(annotationType); + return (type) -> MergedAnnotations.from(type, searchStrategy).isPresent(annotationType); } } diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilterTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilterTests.java index 3976d5bbbd9f..bb12e471a468 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilterTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilterTests.java @@ -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"); * you may not use this file except in compliance with the License. @@ -16,11 +16,14 @@ package org.springframework.boot.validation.beanvalidation; +import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import org.junit.jupiter.api.Test; +import org.springframework.core.annotation.MergedAnnotations.SearchStrategy; + import static org.assertj.core.api.Assertions.assertThat; /** @@ -42,6 +45,19 @@ void byAnnotationWhenClassIsNotAnnotatedIncludes() { 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 { } @@ -51,9 +67,14 @@ static class Annotated { } + @Inherited @Retention(RetentionPolicy.RUNTIME) @interface Indicator { } + static class AnnotatedSuperClass extends Annotated { + + } + }