Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google gRPC Gateway : Overriding response fields?

I am currently working on creating a gRPC service that uses the gRPC gateway / HTTP reverse-proxy to also offer HTTP support. I would like to follow the common conventions of Google API design.

The example I found in the Google API design guide uses google.protobuf.Empty messages for the response of delete methods/RPCs. This is fine, but when I generate a .swagger.json file from the .proto file using protoc-gen-swagger from grpc-gateway, the description of the google.protobuf.Empty message gets pulled in as the description of the response object. This is irrelevant and probably confusing for users of my API, since people using the HTTP gateway aren't using protobufs.

...
"paths": {
  "/v1/{name}": {
    "delete": {
      "summary": "Deletes a book.",
      "operationId": "DeleteBook",
      "responses": {
        "200": {
          "description": "",
          "schema": {
            "$ref": "#/definitions/protobufEmpty"
          }
        }
      },
      ...
    }
  }
},
"definitions": {
  "protobufEmpty": {
    "type": "object",
    "description": "service Foo {\n      rpc Bar(google.protobuf.Empty) returns (google.protobuf.Empty);\n    }\n\nThe JSON representation for `Empty` is empty JSON object `{}`.",
    "title": "A generic empty message that you can re-use to avoid defining duplicated\nempty messages in your APIs. A typical example is to use it as the request\nor the response type of an API method. For instance:"
  }
}

I would say that this is an issue that should be resolved by the protoc-gen-swagger plugin, except that it's really doing what it's supposed to. Is there an HTTP annotation to somehow handle or override the fields in a response? If not, how do people handle this?

like image 610
A. Davidson Avatar asked Aug 10 '18 23:08

A. Davidson


People also ask

Does API gateway support gRPC?

API Gateway provides protocol translation for your gRPC services on Cloud Run allowing clients to use HTTP/JSON to communicate with a gRPC service through the API Gateway. The most common use case is allowing browser clients to talk to gRPC servers without special support from gRPC client libraries.

Does AWS API gateway support gRPC?

API Gateway enables you to provide secure access to gRPC services through a well-defined API configuration. To create a gRPC service, whether you are using API Gateway or not, you specify the interface definition in one or more proto files, which are text files with the .

What is the difference between gRPC and rest?

REST APIs generally use JSON or XML message formats, while gRPC uses protocol buffers. To signal errors, REST APIs use HTTP status codes, while gRPC uses error codes. gRPC's message sizes tend to be dramatically smaller than those of REST APIs.

Does gRPC use HTTP headers?

In gRPC, all requests are HTTP POST requests with content-type prefixed with application/grpc . The remote function ( /ProductInfo/getProduct ) that it invokes is sent as a separate HTTP header. The HTTP request message is sent across the network to the server machine.


1 Answers

You could write a script that removes unwanted elements from the OpenAPI spec after it has been generated by protoc. Something like jq 'del(.definitions.protobufEmpty)' your.swagger.spec.json should work.

like image 154
torkel Avatar answered Oct 05 '22 03:10

torkel