Skip to content

Unused method request/response message rendered in OpenAPI document #5684

Open
@boseabhishek

Description

@boseabhishek

🐛 Bug Report

I'm trying to generate Swagger based OpenAPI docs for my grpc services, proxied to REST endpoints via grpc-gateway. Please see details below:

  1. I have an accounts.proto whoch has two RPC services - Add and Show. I want to ONLY expose Show( GET /v1/account) which is achieved by using annotation google.api.http and not using teh same on Add.

accounts.proto

syntax = "proto3";

package account;

import "google/api/annotations.proto";
import "protoc-gen-openapiv2/options/annotations.proto";

option go_package = "accounts/pkg/account;account";

option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
  info: {
    title: "Account Service";
    version: "1.0";
    contact: {
      name: "some-name";
      url: "https://github.com/some-name";
      email: "[email protected]";
    };
  };
  schemes: [HTTP, HTTPS];
  consumes: ["application/json"];
  produces: ["application/json"];
  responses: {
    key: "default";
    value: {
      description: "An unexpected error occurred.";
    }
  }
};


service AccountService {
  rpc Add(AddRequest) returns (AddResponse);
  rpc Show(ShowRequest) returns (ShowResponse) {
    option (google.api.http) = {
      get: "/v1/account"
    };
  }
}

message Money {
  option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_schema) = {
      json_schema: {
          title: "Money"
          description: "Money data structure"
      };
  };

  int64 amount = 1;
}


message AddRequest {
  Money money = 1;
}

message AddResponse {
}

message ShowRequest {
}

message ShowResponse {
  Money money = 1;
} 
  1. But on the generated swagger.json, I can see unexpected messages in definitions

swagger.json (excerpt)

"definitions": {
    "accountAddResponse": {
      "type": "object"
    },
    "accountMoney": {
      "type": "object",
      "properties": {
        "amount": {
          "type": "string",
          "format": "int64"
        }
      },
      "description": "Money data structure",
      "title": "Money"
    },
    "accountShowResponse": {
      "type": "object",
      "properties": {
        "money": {
          "$ref": "#/definitions/accountMoney"
        }
      }
    },
    "protobufAny": {
      "type": "object",
      "properties": {
        "@type": {
          "type": "string"
        }
      },
      "additionalProperties": {}
    },
    "rpcStatus": {
      "type": "object",
      "properties": {
        "code": {
          "type": "integer",
          "format": "int32"
        },
        "message": {
          "type": "string"
        },
        "details": {
          "type": "array",
          "items": {
            "type": "object",
            "$ref": "#/definitions/protobufAny"
          }
        }
      }
    }
  }

To Reproduce

I use buf generate to generate above file. See buf related files as below:

buf.gen.yaml

version: v1
plugins:
  - plugin: go
    out: gen/go/accounts
    opt:
      - paths=source_relative
  - plugin: go-grpc
    out: gen/go/accounts
    opt:
      - paths=source_relative
  - plugin: grpc-gateway
    out: gen/go/accounts
    opt:
      - paths=source_relative
  - plugin: openapiv2
    out: gen/go/accounts
    opt:
      - ignore_comments=true

buf.yaml

version: v1
deps:
  - buf.build/googleapis/googleapis
  - buf.build/grpc-ecosystem/grpc-gateway 
 

Expected behavior

I get accountAddResponse(shouldn't be as Add is not exposed), protobufAny and rpcStatus as extra and no accountShowRequest

Actual Behavior

I would have expected ONLY accountShowRequest and accountShowResponse.

Your Environment

$ go version
go version go1.24.4 darwin/arm64

Is this a bug or am I missing some settings?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions