ProtoReflectionService class should accept Protobuf schema files with the same name
What version of gRPC-Java are you using?
1.47.0
What is your environment?
macOS Big Sur 11.4, OpenJDK 17.0.3
What did you expect to see?
gRPC services added to a server should be exposed by the ProtoReflectionService class, even if the two services are defined in two different .proto files with the same name.
What did you see instead?
The ProtoReflectionService class only exposes the first service added to a gRPC server (with .addService() method). The issue comes from (ProtoReflectionService.java):
if (!seenFiles.contains(fileDescriptor.getName())) {
seenFiles.add(fileDescriptor.getName());
fileDescriptorsToProcess.add(fileDescriptor);
}
The implementation requires .proto file names to be different, even if they are in completely different source code repositories, directories, or have different namespaces.
Steps to reproduce the bug
- Define a first gRPC service called
InternalServicein a Protobuf schema filesrc/proto/internal/services.proto. - Define a second gRPC service called
ExternalServicein a Protobuf schema file namedsrc/proto/external/services.proto. - Create
Serverand add the two services, and an instance of theProtoReflectionService:
Server server = ServerBuilder
.forPort(8980)
.addService(new InternalService())
.addService(new ExternalService())
.addService(ProtoReflectionService.newInstance())
.build();
- Call one of the rpcs in
ExternalService(i.e. second service added to server) withgrpcurl. - Get back this error:
Error invoking method "specs.external.ExternalService/someRpc": target server does not expose service "specs.external.ExternalService"
Thanks for reporting this. Could you please post both services.proto here? You could simplify them if you wish. We'd like to take a look at the package statements and how services are defined.
@sergiitk thanks for your reply. I think I have found the source of the problem in the grpc code, where (leaf) file names are checked regardless of their path or proto definitions (ProtoReflectionService.java).
if (!seenFiles.contains(fileDescriptor.getName())) {
seenFiles.add(fileDescriptor.getName());
fileDescriptorsToProcess.add(fileDescriptor);
}
However, for completeness, below are the two proto files (simplified).
src/proto/internal/services.proto (used in InternalService):
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.service.internal_query_api";
option java_outer_classname = "InternalQueryGrpcProto";
package specs.grpc_service1;
service InternalService {
...
src/proto/external/services.proto (used in ExternalService):
syntax = "proto3";
option java_multiple_files = true;
option java_package = "com.service.matform.api";
option java_outer_classname = "MatformGrpcProto";
package specs.grpc_service2;
service ExternalService {
...
I suspect the issue may be how the folders are handled by your build system.
I think I have found the source of the problem in the grpc code, where (leaf) file names are checked regardless of their path or proto definitions
That means getName() returns the bare name? getFullName() is an alias of getName() and I don't see any "get folder," so that doesn't seem right.
Testing shows I get the folder in the name. The value of io.grpc.reflection.v1alpha.ServerReflectionProto.getDescriptor().getName():
grpc/reflection/v1alpha/reflection.proto
So the problem is more likely that your two protos are both in the "root" of the proto path. There are three (!) namespaces:
- The proto file path
- The proto package
- The java package
The convention is to have (1) and (2) be mirrors of each other, so at the very least I'd expect the path of the internal service proto to end with specs/grpc_service1/services.proto. C++ uses (1) for imports but (2) for C++ namespace IIRC, so in general having them be the same (with just / replaced with .) makes life easier. To know more precisely what the path should be would depend on your build system configuration (Maven, Gradle, Bazel, etc).
Seems like this is resolved. If not, comment, and it can be reopened.