Skip to content

Commit ffe0ec6

Browse files
committed
Use SearchStrategy in MethodValidationExcludeFilter byAnnotation
Closes gh-30631
1 parent 9b8b870 commit ffe0ec6

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,7 +40,8 @@ public interface MethodValidationExcludeFilter {
4040

4141
/**
4242
* Factory method to create a {@link MethodValidationExcludeFilter} that excludes
43-
* classes by annotation.
43+
* classes by annotation found using an {@link SearchStrategy#INHERITED_ANNOTATIONS
44+
* inherited annotations search strategy}.
4445
* @param annotationType the annotation to check
4546
* @return a {@link MethodValidationExcludeFilter} instance
4647
*/
@@ -50,14 +51,14 @@ static MethodValidationExcludeFilter byAnnotation(Class<? extends Annotation> an
5051

5152
/**
5253
* Factory method to create a {@link MethodValidationExcludeFilter} that excludes
53-
* classes by annotation.
54+
* classes by annotation found using the given search strategy.
5455
* @param annotationType the annotation to check
5556
* @param searchStrategy the annotation search strategy
5657
* @return a {@link MethodValidationExcludeFilter} instance
5758
*/
5859
static MethodValidationExcludeFilter byAnnotation(Class<? extends Annotation> annotationType,
5960
SearchStrategy searchStrategy) {
60-
return (type) -> MergedAnnotations.from(type, SearchStrategy.SUPERCLASS).isPresent(annotationType);
61+
return (type) -> MergedAnnotations.from(type, searchStrategy).isPresent(annotationType);
6162
}
6263

6364
}

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/validation/beanvalidation/MethodValidationExcludeFilterTests.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,11 +16,14 @@
1616

1717
package org.springframework.boot.validation.beanvalidation;
1818

19+
import java.lang.annotation.Inherited;
1920
import java.lang.annotation.Retention;
2021
import java.lang.annotation.RetentionPolicy;
2122

2223
import org.junit.jupiter.api.Test;
2324

25+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
26+
2427
import static org.assertj.core.api.Assertions.assertThat;
2528

2629
/**
@@ -42,6 +45,19 @@ void byAnnotationWhenClassIsNotAnnotatedIncludes() {
4245
assertThat(filter.isExcluded(Plain.class)).isFalse();
4346
}
4447

48+
@Test
49+
void byAnnotationWhenSuperclassIsAnnotatedWithInheritedAnnotationExcludes() {
50+
MethodValidationExcludeFilter filter = MethodValidationExcludeFilter.byAnnotation(Indicator.class);
51+
assertThat(filter.isExcluded(AnnotatedSuperClass.class)).isTrue();
52+
}
53+
54+
@Test
55+
void byAnnotationWithDirectSearchStrategyWhenSuperclassIsAnnotatedWithInheritedAnnotationIncludes() {
56+
MethodValidationExcludeFilter filter = MethodValidationExcludeFilter.byAnnotation(Indicator.class,
57+
SearchStrategy.DIRECT);
58+
assertThat(filter.isExcluded(AnnotatedSuperClass.class)).isFalse();
59+
}
60+
4561
static class Plain {
4662

4763
}
@@ -51,9 +67,14 @@ static class Annotated {
5167

5268
}
5369

70+
@Inherited
5471
@Retention(RetentionPolicy.RUNTIME)
5572
@interface Indicator {
5673

5774
}
5875

76+
static class AnnotatedSuperClass extends Annotated {
77+
78+
}
79+
5980
}

0 commit comments

Comments
 (0)