From ed50bf2494bdab141b57f84f1eb7a02f5ce8e521 Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 5 Nov 2019 10:51:36 +0000 Subject: [PATCH] Honour EndpointFilter configured on an endpoint's superclass MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously, @EndpointFilter would only have an effect when used as an annotation or meta-annotation on the endpoint class itself. It would have no effect when used on a super-class of the endpoint bean's class. This commit updates EndpointDiscoverer so that an @EndpointFilter annotation or meta-annotation on a super-class will be found and applied to the discovery process. This is achieved by using find… rather than get… when retrieving the attributes for the EndpointFilter annotation. Fixes gh-17866 --- .../annotation/EndpointDiscoverer.java | 4 ++-- .../annotation/EndpointDiscovererTests.java | 22 ++++++++++++++++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java index a1ff0f54af7..61e93126d9a 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscoverer.java @@ -423,8 +423,8 @@ public abstract class EndpointDiscoverer, O exten } private Class getFilter(Class type) { - AnnotationAttributes attributes = AnnotatedElementUtils.getMergedAnnotationAttributes(type, - FilteredEndpoint.class); + AnnotationAttributes attributes = AnnotatedElementUtils.findMergedAnnotationAttributes(type, + FilteredEndpoint.class, false, true); if (attributes == null) { return null; } diff --git a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java index 94a8f065ece..6154cd9ba6f 100644 --- a/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java +++ b/spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/annotation/EndpointDiscovererTests.java @@ -209,7 +209,8 @@ public class EndpointDiscovererTests { load(SpecializedEndpointsConfiguration.class, (context) -> { SpecializedEndpointDiscoverer discoverer = new SpecializedEndpointDiscoverer(context); Map endpoints = mapEndpoints(discoverer.getEndpoints()); - assertThat(endpoints).containsOnlyKeys(EndpointId.of("test"), EndpointId.of("specialized")); + assertThat(endpoints).containsOnlyKeys(EndpointId.of("test"), EndpointId.of("specialized"), + EndpointId.of("specialized-superclass")); }); } @@ -252,7 +253,7 @@ public class EndpointDiscovererTests { load(SpecializedEndpointsConfiguration.class, (context) -> { EndpointFilter filter = (endpoint) -> { EndpointId id = endpoint.getEndpointId(); - return !id.equals(EndpointId.of("specialized")); + return !id.equals(EndpointId.of("specialized")) && !id.equals(EndpointId.of("specialized-superclass")); }; SpecializedEndpointDiscoverer discoverer = new SpecializedEndpointDiscoverer(context, Collections.singleton(filter)); @@ -401,7 +402,8 @@ public class EndpointDiscovererTests { } - @Import({ TestEndpoint.class, SpecializedTestEndpoint.class, SpecializedExtension.class }) + @Import({ TestEndpoint.class, SpecializedTestEndpoint.class, SpecializedSuperclassTestEndpoint.class, + SpecializedExtension.class }) static class SpecializedEndpointsConfiguration { } @@ -494,6 +496,20 @@ public class EndpointDiscovererTests { } + @SpecializedEndpoint(id = "specialized-superclass") + static class AbstractFilteredEndpoint { + + } + + static class SpecializedSuperclassTestEndpoint extends AbstractFilteredEndpoint { + + @ReadOperation + public Object getAll() { + return null; + } + + } + static class SubSpecializedTestEndpoint extends SpecializedTestEndpoint { @ReadOperation