From 612348ec265e6b0e184094b883adcadd4e3d8682 Mon Sep 17 00:00:00 2001 From: Clement Escoffier Date: Thu, 24 Mar 2022 15:33:27 +0100 Subject: [PATCH] Register Protobuf enums in the native executable. Protobuf enums generate enum classes. Fortunately, we can locate them with Jandex using the ProtocolMessageEnum interface, as the generated enums implement this interface. Fix https://github.com/quarkusio/quarkus/issues/24510. --- .../deployment/GrpcCommonProcessor.java | 8 +++++++ .../grpc/common/deployment/GrpcDotNames.java | 2 ++ .../grpc-plain-text-mutiny/pom.xml | 21 ++-------------- .../examples/hello/HelloWorldService.java | 24 +++++++++++++++++++ .../src/main/proto/helloworld.proto | 12 ++++++++++ .../examples/hello/HelloWorldServiceTest.java | 15 ++++++++++++ 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java b/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java index 31d637e0535e6..61ec420a56ee4 100644 --- a/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java +++ b/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcCommonProcessor.java @@ -25,6 +25,14 @@ public void configureNativeExecutable(CombinedIndexBuildItem combinedIndex, for (ClassInfo message : messages) { reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true, message.name().toString())); } + + // We also need to include enums. + Collection enums = combinedIndex.getIndex() + .getAllKnownSubclasses(GrpcDotNames.PROTOCOL_MESSAGE_ENUM); + for (ClassInfo en : enums) { + reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true, en.name().toString())); + } + Collection builders = combinedIndex.getIndex().getAllKnownSubclasses(GrpcDotNames.MESSAGE_BUILDER); for (ClassInfo builder : builders) { reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true, builder.name().toString())); diff --git a/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcDotNames.java b/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcDotNames.java index 12e5ca80aafec..6cf9cfc32af7f 100644 --- a/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcDotNames.java +++ b/extensions/grpc-common/deployment/src/main/java/io/quarkus/grpc/common/deployment/GrpcDotNames.java @@ -3,6 +3,7 @@ import org.jboss.jandex.DotName; import com.google.protobuf.GeneratedMessageV3; +import com.google.protobuf.ProtocolMessageEnum; import io.grpc.LoadBalancerProvider; import io.grpc.NameResolverProvider; @@ -10,6 +11,7 @@ public class GrpcDotNames { static final DotName MESSAGE_BUILDER = DotName.createSimple(GeneratedMessageV3.Builder.class.getName()); static final DotName GENERATED_MESSAGE_V3 = DotName.createSimple(GeneratedMessageV3.class.getName()); + static final DotName PROTOCOL_MESSAGE_ENUM = DotName.createSimple(ProtocolMessageEnum.class.getName()); static final DotName NAME_RESOLVER_PROVIDER = DotName.createSimple(NameResolverProvider.class.getName()); static final DotName LOAD_BALANCER_PROVIDER = DotName.createSimple(LoadBalancerProvider.class.getName()); } diff --git a/integration-tests/grpc-plain-text-mutiny/pom.xml b/integration-tests/grpc-plain-text-mutiny/pom.xml index 1c7317764a405..11de57b23b43b 100644 --- a/integration-tests/grpc-plain-text-mutiny/pom.xml +++ b/integration-tests/grpc-plain-text-mutiny/pom.xml @@ -16,11 +16,7 @@ io.quarkus - quarkus-resteasy-jackson - - - io.quarkus - quarkus-resteasy-mutiny + quarkus-resteasy-reactive-jackson io.quarkus @@ -67,20 +63,7 @@ io.quarkus - quarkus-resteasy-jackson-deployment - ${project.version} - pom - test - - - * - * - - - - - io.quarkus - quarkus-resteasy-mutiny-deployment + quarkus-resteasy-reactive-jackson-deployment ${project.version} pom test diff --git a/integration-tests/grpc-plain-text-mutiny/src/main/java/io/quarkus/grpc/examples/hello/HelloWorldService.java b/integration-tests/grpc-plain-text-mutiny/src/main/java/io/quarkus/grpc/examples/hello/HelloWorldService.java index af5c27562db06..0d5bd49c82a70 100644 --- a/integration-tests/grpc-plain-text-mutiny/src/main/java/io/quarkus/grpc/examples/hello/HelloWorldService.java +++ b/integration-tests/grpc-plain-text-mutiny/src/main/java/io/quarkus/grpc/examples/hello/HelloWorldService.java @@ -4,6 +4,7 @@ import examples.HelloReply; import examples.HelloRequest; +import examples.LanguageSpec; import examples.MutinyGreeterGrpc; import io.quarkus.grpc.GrpcService; import io.quarkus.grpc.RegisterInterceptor; @@ -22,4 +23,27 @@ public Uni sayHello(HelloRequest request) { return Uni.createFrom().item("Hello " + name) .map(res -> HelloReply.newBuilder().setMessage(res).setCount(count).build()); } + + @Override + public Uni greeting(LanguageSpec request) { + return Uni.createFrom().item(() -> { + String res = null; + switch (request.getSelectedLanguage()) { + case FRENCH: + res = "Bonjour!"; + break; + case SPANISH: + res = "Hola!"; + break; + case ENGLISH: + res = "Hello!"; + break; + case UNRECOGNIZED: + res = "Blurp!"; + break; + } + return res; + }) + .map(res -> HelloReply.newBuilder().setMessage(res).build()); + } } diff --git a/integration-tests/grpc-plain-text-mutiny/src/main/proto/helloworld.proto b/integration-tests/grpc-plain-text-mutiny/src/main/proto/helloworld.proto index 89ccb456d8308..f2eac36c33d96 100644 --- a/integration-tests/grpc-plain-text-mutiny/src/main/proto/helloworld.proto +++ b/integration-tests/grpc-plain-text-mutiny/src/main/proto/helloworld.proto @@ -40,6 +40,9 @@ package helloworld; service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {} + + rpc Greeting (LanguageSpec) returns (HelloReply) {} + } // The request message containing the user's name. @@ -52,3 +55,12 @@ message HelloReply { string message = 1; int32 count = 2; } + +message LanguageSpec { + enum Language { + ENGLISH = 0; + FRENCH = 1; + SPANISH = 2; + }; + Language selected_language = 1; +} diff --git a/integration-tests/grpc-plain-text-mutiny/src/test/java/io/quarkus/grpc/examples/hello/HelloWorldServiceTest.java b/integration-tests/grpc-plain-text-mutiny/src/test/java/io/quarkus/grpc/examples/hello/HelloWorldServiceTest.java index 94689891b0928..5a9a6f1d52b26 100644 --- a/integration-tests/grpc-plain-text-mutiny/src/test/java/io/quarkus/grpc/examples/hello/HelloWorldServiceTest.java +++ b/integration-tests/grpc-plain-text-mutiny/src/test/java/io/quarkus/grpc/examples/hello/HelloWorldServiceTest.java @@ -12,6 +12,7 @@ import examples.GreeterGrpc; import examples.HelloReply; import examples.HelloRequest; +import examples.LanguageSpec; import examples.MutinyGreeterGrpc; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; @@ -49,4 +50,18 @@ public void testHelloWorldServiceUsingMutinyStub() { assertThat(reply.getMessage()).isEqualTo("Hello neo-mutiny"); } + @Test + public void testEnumSupport() { + GreeterGrpc.GreeterBlockingStub client = GreeterGrpc.newBlockingStub(channel); + HelloReply reply = client + .greeting(LanguageSpec.newBuilder().setSelectedLanguage(LanguageSpec.Language.FRENCH).build()); + assertThat(reply.getMessage()).isEqualTo("Bonjour!"); + reply = client + .greeting(LanguageSpec.newBuilder().setSelectedLanguage(LanguageSpec.Language.SPANISH).build()); + assertThat(reply.getMessage()).isEqualTo("Hola!"); + reply = client + .greeting(LanguageSpec.newBuilder().setSelectedLanguage(LanguageSpec.Language.ENGLISH).build()); + assertThat(reply.getMessage()).isEqualTo("Hello!"); + } + }