Search code examples
protocol-buffersgrpcprotocbuf

use grpc reflection service to build static model


I am building a client to a specific grpc application, that exposes also the reflection service (so, for example, I can use grpc_cli to check the exposed call/types).

ATM I don't have direct access to the .proto files that have been used to build the service. I could ask for a copy of them, or to share the .proto files in a dedicated shared space, but I'm wondering if it's possible to instrument protoc (or buf) to generate the source code of the model for my application fetching the informations directly from the reflection service.

Or at least some way to generate the .proto files (or the descriptor...) locally (then it's just a two-step solution)

Is it possible? How?

(If that could help, the server has been written in typescript and my client is in java)


Solution

  • The reflection API uses descriptors, and grpcurl can export them as a google.protobuf.FileDescriptorSet. Both protoc and the buf CLI can use the binary descriptors to generate code.

    Here is an example:

    grpcurl -protoset-out eliza-descriptors.bin demo.connectrpc.com:443 describe
    

    The command retrieves descriptors via reflection from a demo service (source at https://github.com/connectrpc/examples-go), and writes them to a file.

    The following command generates code from the descriptors.

    buf generate eliza-descriptors.bin
    

    Comments in proto files won't make it, but besides that, the result is identical to what you would get if you had the original proto sources.