Google gRPC Gateway:覆盖响应字段?

A. *_*son 8 google-api protocol-buffers swagger grpc grpc-gateway

我目前正致力于创建一个使用gRPC网关/ HTTP反向代理的gRPC服务,以提供HTTP支持.我想遵循Google API设计的通用惯例.

我在Google API设计指南中找到的示例使用google.protobuf.Empty消息来表示删除方法/ RPC的响应.这很好,但是当我使用来自grpc-gateway的protoc- gen-swagger从文件生成.swagger.json文件时,消息的描述将被作为响应对象的描述拉入.这与我的API用户无关,可能会让人感到困惑,因为使用HTTP网关的人不使用protobufs..protogoogle.protobuf.Empty

...
"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:"
  }
}
Run Code Online (Sandbox Code Playgroud)

我想说这是一个应该由protoc-gen-swagger插件解决的问题,除了它确实在做它应该做的事情.是否有HTTP注释以某种方式处理或覆盖响应中的字段?如果没有,人们如何处理这个?

tor*_*kel 1

您可以编写一个脚本,在 OpenAPI 规范生成后将不需要的元素删除protoc。类似的东西jq 'del(.definitions.protobufEmpty)' your.swagger.spec.json应该有效。