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

protoc-gen-openapiv2: YAML output of examples is wrong #3095

Closed
nipeharefa opened this issue Dec 26, 2022 · 6 comments · Fixed by #3106
Closed

protoc-gen-openapiv2: YAML output of examples is wrong #3095

nipeharefa opened this issue Dec 26, 2022 · 6 comments · Fixed by #3106

Comments

@nipeharefa
Copy link

🐛 Bug Report

Hello, this is my first issue, I just try using protoc-gen-openapiv2 and write some proto but I found when write the example of response and render it, the output will render []byte.

Sorry for my english.

To Reproduce

(Write your steps here:)

  1. Write a example like this one:
examples: {
        key: "application/json"
        value: "{\"value\": \"the input value\"}"
      }
  1. After generate, the value is like []byte:

image

My example proto

service Signup {
    rpc Signup (SignupRequest) returns (SampleResponse) {
      option (google.api.http) = {
        post: "/v1/signup",
        body: "*"
      };
      option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
        consumes: [""]
        responses: {
            key: "200"
            value: {
                description: "Success"
            }
        }
        responses: {
            key: "500"
            value: {
                description: "internal Server Error",
                schema: {
                  json_schema: {
                    ref: ".Error";
                  }
                }
                examples: {
                  key: "application/json"
                  value: "{\"value\": \"the input value\"}"
                }
            }
        }
      };
    }
}

i;m using buf to generate, this is my configuration:

version: v1
managed:
  enabled: true
  go_package_prefix:
    default: github.com/example/example-proto
plugins:
  - remote: buf.build/grpc-ecosystem/plugins/openapiv2:v2.15.0
    opt:
      - merge_file_name=config
      - allow_merge
      - use_allof_for_refs
    out: ../../gen/openapiv2
@johanbrandhorst
Copy link
Collaborator

Hi, thanks for your issue! Looks like this is a special case for the application/json type: https://github.com/grpc-ecosystem/grpc-gateway/blob/257af994261d2395689810c48b3a3fb8ad7b318a/protoc-gen-openapiv2/internal/genopenapi/template.go#L2762C28-L2778. What does the swagger.json look like?

@nipeharefa
Copy link
Author

hi @johanbrandhorst

paths:
  /_internal/user/{userId}:
    get:
      summary: Get tuples from the store that matches a query, without following userset rewrite rules
      operationId: GetUserByID
      responses:
        "200":
          description: A successful response.
          schema:
            $ref: '#/definitions/UserResponseInternal'
        "500":
          description: Internal Server Error
          schema:
            $ref: '#/definitions/Error'
          examples:
            application/json:
              - 123
              - 34
              - 118
              - 97
              - 108
              - 117
              - 101
              - 34
              - 58
              - 32
              - 34
              - 116
              - 104
              - 101
              - 32
              - 105
              - 110
              - 112
              - 117
              - 116
              - 32
              - 118
              - 97
              - 108
              - 117
              - 101
              - 34
              - 125
        default:
          description: An unexpected error response.
          schema:
            $ref: '#/definitions/rpcStatus'
      parameters:
        - name: userId
          in: path
          required: true
          type: string
      tags:
        - User

@johanbrandhorst
Copy link
Collaborator

Hm, that looks like YAML, I wonder if this is a problem with the yaml marshaler. Can you try to generate a JSON version?

@nipeharefa
Copy link
Author

@johanbrandhorst you're right, i just change output to JSON, and look's good 👍 .

"get": {
        "summary": "Get tuples from the store that matches a query, without following userset rewrite rules",
        "operationId": "GetUserByID",
        "responses": {
          "200": {
            "description": "A successful response.",
            "schema": {
              "$ref": "#/definitions/UserResponseInternal"
            }
          },
          "500": {
            "description": "Internal Server Error",
            "schema": {
              "$ref": "#/definitions/Error"
            },
            "examples": {
              "application/json": {
                "value": "the input value"
              }
            }
          },
          "default": {
            "description": "An unexpected error response.",
            "schema": {
              "$ref": "#/definitions/rpcStatus"
            }
          }
        },
        "parameters": [
          {
            "name": "userId",
            "in": "path",
            "required": true,
            "type": "string"
          }
        ],
        "tags": [
          "User"
        ]
      }

@johanbrandhorst
Copy link
Collaborator

Great, that definitely narrows down the problem to the YAML output. The logic for this lives here:

case FormatYAML:
enc := yaml.NewEncoder(w)
enc.SetIndent(2)
return enc, nil

We may need to add some sort of workaround for json.RawMessage in the yaml marshaler. Thoughts @hedhyw?

@johanbrandhorst johanbrandhorst changed the title Invalid example output in operation protoc-gen-openapiv2: YAML output of examples is wrong Jan 3, 2023
hedhyw added a commit to hedhyw/grpc-gateway that referenced this issue Jan 5, 2023
hedhyw added a commit to hedhyw/grpc-gateway that referenced this issue Jan 5, 2023
@hedhyw
Copy link
Contributor

hedhyw commented Jan 5, 2023

@nipeharefa I've just fixed this in #3106, please try to use it from my branch and test it.

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

Successfully merging a pull request may close this issue.

3 participants