Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generated swagger.yaml is corrupted in openapiv2 v2.17.0+: Path item becomes string instead of object #3557

Closed
qawatake opened this issue Aug 30, 2023 · 3 comments · Fixed by #3566

Comments

@qawatake
Copy link
Contributor

🐛 Bug Report

Starting from version v2.17.0 of openapiv2, the generated swagger.yaml file is corrupted. Specifically, where the path item should be an object, it has become a string.

To Reproduce

Execute the following using openapiv2 v2.17.1:

protoc -I . --openapiv2_out ./gen \
    --openapiv2_opt=output_format=yaml \
    your/service/v1/your_service.proto

For this, let's assume your_service.proto is as follows:

syntax = "proto3";
package your.service.v1;

import "google/api/annotations.proto";

option go_package = "github.com/yourorg/yourprotos/gen/go/your/service/v1";

message StringMessage {
  string value = 1;
}

service YourService {
  rpc Echo(StringMessage) returns (StringMessage) {
    option (google.api.http) = {post: "/api/echo"};
  }
}

Expected behavior

The file generated with openapiv2 v.2.6.2 is as follows:

swagger: "2.0"
info:
  title: your/service/v1/your_service.proto
  version: version not set
tags:
  - name: YourService
consumes:
  - application/json
produces:
  - application/json
paths:
  /api/echo:
    post:
      operationId: YourService_Echo
      responses:
        "200":
          description: A successful response.
          schema:
            $ref: '#/definitions/v1StringMessage'
        default:
          description: An unexpected error response.
          schema:
            $ref: '#/definitions/rpcStatus'
      parameters:
        - name: value
          in: query
          required: false
          type: string
      tags:
        - YourService
definitions:
  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'
  v1StringMessage:
    type: object
    properties:
      value:
        type: string

Actual Behavior

The file generated with openapiv2 v2.17.1 is as follows. The path item corresponding to /api/echo has turned into a string.

swagger: "2.0"
info:
  title: your/service/v1/your_service.proto
  version: version not set
tags:
  - name: YourService
consumes:
  - application/json
produces:
  - application/json
paths:
  /api/echo: |
    post:
        operationId: YourService_Echo
        responses:
            "200":
                description: A successful response.
                schema:
                    $ref: '#/definitions/v1StringMessage'
            default:
                description: An unexpected error response.
                schema:
                    $ref: '#/definitions/rpcStatus'
        parameters:
            - name: value
              in: query
              required: false
              type: string
        tags:
            - YourService
definitions:
  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'
  v1StringMessage:
    type: object
    properties:
      value:
        type: string

Your Environment

Mac OS - Darwin Kernel Version 21.3.0: Wed Jan 5 21:37:58 PST 2022; root:xnu-8019.80.24~20/RELEASE_X86_64 x86_64

@qawatake
Copy link
Contributor Author

I think it can probably be fixed like this ↓. However, this will cause existing tests to fail. I suspect it's a bug in go-yaml v3.

qawatake@146e09a

$ go test -v -run YAMLIndent ./protoc-gen-openapiv2/internal/genopenapi
=== RUN   TestGeneratedYAMLIndent
2023/08/30 22:27:50 ERROR: no message found: Status
    generator_test.go:251: failed to generate targets: failed to encode OpenAPI for exampleproto/v1/exampleproto.proto: yaml: line 8: did not find expected key
--- FAIL: TestGeneratedYAMLIndent (0.00s)
FAIL
FAIL    github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2/internal/genopenapi    0.219s
FAIL

@jruckert
Copy link

jruckert commented Sep 1, 2023

Can confirm we are experiencing this also.

@johanbrandhorst
Copy link
Collaborator

Thanks for reporting this issue. Please feel free to submit a PR with the proposed change and we can work out exactly what if any test changes will need to be made. Thank you!

johanbrandhorst pushed a commit that referenced this issue Sep 9, 2023
* fix

* implement toYAMLNode

* add comment

* check len

* add test

* add comment

* rename

* update

* generate
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants