Question

is there a way to dynamically construct request message of a grpc method based from proto?

I am new to gRPC and would like to do input validation of a gRPC service. The testing would be carried out on a running server, and I would use grpcurl to invoke each of the method with dummy input within a JSON filed.

There are number of fields and nested fields on request message, I been searching for any tool to dynamically construct an example JSON based on the protobuf, but seems there is nothing out there. The closest one I have found is protoc-gen-jsonschema which translate protobuf message to json schema and later construct an example JSON programatically.

 2  27  2
1 Jan 1970

Solution

 0

This is an interesting question and I don't have a definitive answer.

My hunch is that gRPCurl (an excellent tool) may be limited for this purpose:

  1. Automation;
  2. Default values;
  3. Type "mismatch" issues
  4. Field order issues

I think you would overcome these issues using a programming language (Python, Go, Rust etc.) instead.

Buf is doing interesting work w/ Protobufs and has created protovalidate as a mechanism that:

  • Adds annotations to Protobuf sources to specify validation constraints
  • Provides libraries that add validation to the generated stubs.

This solves part of your problem.

The other part is to test the result. Fuzzing (and see Google's Fuzzing forum) is an increasingly popular testing tool.

Fuzzing protobufs has some differences but there are languages|tools that you can use to fuzz protobufs:

  1. Google's Python atheris which includes Structure-aware Fuzzing with Protobufs
  2. Go fuzzing
  3. etc.
2024-07-18
DazWilkin

Solution

 0

I'm not aware of any tool creating JSON structures from Protobuf, but I suggest a different approach.

Instead of defining Protobuf directly you can define your API in OpenAPI format, then use tools to generate Protobuf structures. You can find more info about that approach here: How do you convert an OpenAPI Spec (Swagger 2.0) to proto3?

One of the greatest advantage of this solution is that you can define stubs for Javascript and all other languages supported by Protobuf at the same time.

2024-07-25
Adam