From d2b39f1c5fbdb1d8db67639fdab6fd831b373b9b Mon Sep 17 00:00:00 2001 From: Ales Justin Date: Tue, 12 Sep 2023 13:47:18 +0200 Subject: [PATCH] Fix issue #35634 - missing interfaces check when looking for global server interceptors. --- .../grpc/deployment/GrpcInterceptors.java | 35 +++++++++++-------- .../interceptors/PriorityImplInterceptor.java | 24 +++++++++++++ .../interceptors/PriorityInterceptor.java | 8 +++++ .../HelloWorldEndpointTestBase.java | 1 + 4 files changed, 54 insertions(+), 14 deletions(-) create mode 100644 integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityImplInterceptor.java create mode 100644 integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityInterceptor.java diff --git a/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcInterceptors.java b/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcInterceptors.java index 07400477fc693..51ec008889881 100644 --- a/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcInterceptors.java +++ b/extensions/grpc/deployment/src/main/java/io/quarkus/grpc/deployment/GrpcInterceptors.java @@ -53,34 +53,41 @@ static GrpcInterceptors gatherInterceptors(IndexView index, DotName interceptorI private static Set allGlobalInterceptors(IndexView index, DotName interceptorInterface) { Set result = new HashSet<>(); for (AnnotationInstance instance : index.getAnnotations(GLOBAL_INTERCEPTOR)) { - ClassInfo classInfo = classInfo(index, instance.target()); - if (isAssignableFrom(index, classInfo, interceptorInterface)) { - result.add(classInfo.name()); + DotName className = className(instance.target()); + if (isAssignableFrom(index, className, interceptorInterface)) { + result.add(className); } } return result; } - private static ClassInfo classInfo(IndexView index, AnnotationTarget target) { + private static DotName className(AnnotationTarget target) { if (target.kind() == CLASS) { - return target.asClass(); + return target.asClass().name(); } else if (target.kind() == METHOD) { - return index.getClassByName(target.asMethod().returnType().name()); + return target.asMethod().returnType().name(); } return null; } - private static boolean isAssignableFrom(IndexView index, ClassInfo classInfo, DotName interceptorInterface) { - if (classInfo == null) { + private static boolean isAssignableFrom(IndexView index, DotName className, DotName interceptorInterface) { + if (className == null) { return false; } - if (classInfo.interfaceNames().contains(interceptorInterface)) { - return true; - } - if (classInfo.superName() == null) { - return false; + + ClassInfo classInfo = index.getClassByName(className); + List interfaceNames = classInfo.interfaceNames(); + for (DotName in : interfaceNames) { + if (in.equals(interceptorInterface)) { + return true; + } + boolean result = isAssignableFrom(index, in, interceptorInterface); + if (result) { + return true; + } } - return isAssignableFrom(index, index.getClassByName(classInfo.superName()), interceptorInterface); + + return isAssignableFrom(index, classInfo.superName(), interceptorInterface); } } diff --git a/integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityImplInterceptor.java b/integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityImplInterceptor.java new file mode 100644 index 0000000000000..80c5c718fb725 --- /dev/null +++ b/integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityImplInterceptor.java @@ -0,0 +1,24 @@ +package io.quarkus.grpc.examples.interceptors; + +import jakarta.enterprise.context.ApplicationScoped; + +import io.grpc.Metadata; +import io.grpc.ServerCall; +import io.grpc.ServerCallHandler; +import io.quarkus.grpc.GlobalInterceptor; + +@ApplicationScoped +@GlobalInterceptor +public class PriorityImplInterceptor implements PriorityInterceptor { + @Override + public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, + ServerCallHandler next) { + HelloWorldEndpoint.invoked.add(getClass().getName()); + return next.startCall(call, headers); + } + + @Override + public int getPriority() { + return 0; + } +} diff --git a/integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityInterceptor.java b/integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityInterceptor.java new file mode 100644 index 0000000000000..9ae7d6eedd0d1 --- /dev/null +++ b/integration-tests/grpc-interceptors/src/main/java/io/quarkus/grpc/examples/interceptors/PriorityInterceptor.java @@ -0,0 +1,8 @@ +package io.quarkus.grpc.examples.interceptors; + +import jakarta.enterprise.inject.spi.Prioritized; + +import io.grpc.ServerInterceptor; + +public interface PriorityInterceptor extends ServerInterceptor, Prioritized { +} diff --git a/integration-tests/grpc-interceptors/src/test/java/io/quarkus/grpc/example/interceptors/HelloWorldEndpointTestBase.java b/integration-tests/grpc-interceptors/src/test/java/io/quarkus/grpc/example/interceptors/HelloWorldEndpointTestBase.java index 78c3ff85cf9b4..1ed65f70184fb 100644 --- a/integration-tests/grpc-interceptors/src/test/java/io/quarkus/grpc/example/interceptors/HelloWorldEndpointTestBase.java +++ b/integration-tests/grpc-interceptors/src/test/java/io/quarkus/grpc/example/interceptors/HelloWorldEndpointTestBase.java @@ -21,6 +21,7 @@ public void testHelloWorldServiceUsingBlockingStub() { assertThat(invoked).containsExactlyInAnyOrder( "io.quarkus.grpc.examples.interceptors.ClientInterceptors$TypeTarget", "io.quarkus.grpc.examples.interceptors.ClientInterceptors$MethodTarget", + "io.quarkus.grpc.examples.interceptors.PriorityImplInterceptor", "io.quarkus.grpc.examples.interceptors.ServerInterceptors$TypeTarget", "io.quarkus.grpc.examples.interceptors.ServerInterceptors$MethodTarget");