Skip to content

Commit

Permalink
Register Protobuf enums in the native executable.
Browse files Browse the repository at this point in the history
Protobuf enums generate enum classes.
Fortunately, we can locate them with Jandex using the ProtocolMessageEnum interface, as the generated enums implement this interface.

Fix quarkusio#24510.
  • Loading branch information
cescoffier committed Mar 24, 2022
1 parent 533bfff commit 612348e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClassInfo> enums = combinedIndex.getIndex()
.getAllKnownSubclasses(GrpcDotNames.PROTOCOL_MESSAGE_ENUM);
for (ClassInfo en : enums) {
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true, en.name().toString()));
}

Collection<ClassInfo> builders = combinedIndex.getIndex().getAllKnownSubclasses(GrpcDotNames.MESSAGE_BUILDER);
for (ClassInfo builder : builders) {
reflectiveClass.produce(new ReflectiveClassBuildItem(true, true, true, builder.name().toString()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import org.jboss.jandex.DotName;

import com.google.protobuf.GeneratedMessageV3;
import com.google.protobuf.ProtocolMessageEnum;

import io.grpc.LoadBalancerProvider;
import io.grpc.NameResolverProvider;

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());
}
21 changes: 2 additions & 19 deletions integration-tests/grpc-plain-text-mutiny/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@
<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
Expand Down Expand Up @@ -67,20 +63,7 @@
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>*</groupId>
<artifactId>*</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-resteasy-mutiny-deployment</artifactId>
<artifactId>quarkus-resteasy-reactive-jackson-deployment</artifactId>
<version>${project.version}</version>
<type>pom</type>
<scope>test</scope>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,4 +23,27 @@ public Uni<HelloReply> sayHello(HelloRequest request) {
return Uni.createFrom().item("Hello " + name)
.map(res -> HelloReply.newBuilder().setMessage(res).setCount(count).build());
}

@Override
public Uni<HelloReply> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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!");
}

}

0 comments on commit 612348e

Please sign in to comment.