Skip to content

protoc-gen-openapiv2: Unnecessary package prefixes in definitions when no actual collision exists #5799

@ajorgensen

Description

@ajorgensen

🐛 Bug Report

The protoc-gen-openapiv2 generator unnecessarily over-specifies definition names when using the package or simple naming strategy. When a package imports another package that contains a message with the same name as a message in the current package, the generator prefixes the definition name with the package name even when the imported message is not referenced in the generated OpenAPI specification, creating no risk of naming collision.

To Reproduce

Create the following proto files:

./foo.proto

syntax = "proto3";

package foo;

import "other/foo.proto";

option go_package = "github.com/ajorgensen/example/foo;foo";

service FooService {
  rpc Bar(Foo) returns (Foo);
}

message Foo {
  other.Other other = 3;
}

./other/foo.proto

syntax = "proto3";

package other;

option go_package = "github.com/ajorgensen/example/other;other";

message Foo {
  string baz = 1;
}

message Other {
  string bar = 1;
}

Generate OpenAPI specification using:

protoc --proto_path=. *.proto \
  --openapiv2_opt allow_merge=true,merge_file_name=openapi,openapi_naming_strategy=package \
  --openapiv2_out generated/proto/

Actual Behavior

The generator produces the following OpenAPI specification:

{
  "swagger": "2.0",
  "info": {
    "title": "foo.proto",
    "version": "version not set"
  },
  "tags": [
    {
      "name": "FooService"
    }
  ],
  "consumes": [
    "application/json"
  ],
  "produces": [
    "application/json"
  ],
  "paths": {},
  "definitions": {
    "Any": {
      "type": "object",
      "properties": {
        "@type": {
          "type": "string"
        }
      },
      "additionalProperties": {}
    },
    "Other": {
      "type": "object",
      "properties": {
        "bar": {
          "type": "string"
        }
      }
    },
    "Status": {
      "type": "object",
      "properties": {
        "code": {
          "type": "integer",
          "format": "int32"
        },
        "message": {
          "type": "string"
        },
        "details": {
          "type": "array",
          "items": {
            "type": "object",
            "$ref": "#/definitions/Any"
          }
        }
      }
    },
    "foo.Foo": {
      "type": "object",
      "properties": {
        "other": {
          "$ref": "#/definitions/Other"
        }
      }
    }
  }
}

Note that the foo.Foo message is unnecessarily prefixed with the package name.

Expected Behavior

Since the other.Foo message is never referenced in the generated OpenAPI specification, there is no naming collision risk. The definition should be named simply Foo instead of foo.Foo:

"definitions": {
  "Foo": {
    "type": "object",
    "properties": {
      "other": {
        "$ref": "#/definitions/Other"
      }
    }
  }
}

Environment

protoc-gen-openapiv2 --version
Version v2.27.1, commit unknown, built at unknown

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