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

Generate enum with values when using protoc-gen-openapi #255

Open
Hangzhi opened this issue Aug 12, 2021 · 3 comments
Open

Generate enum with values when using protoc-gen-openapi #255

Hangzhi opened this issue Aug 12, 2021 · 3 comments

Comments

@Hangzhi
Copy link

Hangzhi commented Aug 12, 2021

Thanks for this helpful tool.

I come across this issue when I'm using protoc-gen-openapi plugin to generate OpenAPI3 specification from protobuf definition.

I have a State type and fields of State type are marked with enum format.

enum State {
    state1             = 0;
    state2             = 1;
    state3             = 2;
}
state:
    type: integer
    format: enum

However, for enum in OpenAPI3 should be

state:
 type: integer
 enum:
        - 0
        - 1
        - 2

Is there any feature that we can configure to generate enum schema?

@renyijiu
Copy link

EnumType: flags.String("enum_type", "integer", `type for enum serialization. Use "string" for string-based serialization`),

I checked the source code and found that this feature is already supported, but I don't know how to pass this parameter to the protoc command to make it work, hope it helps. And I tested it in the buf plugin and it works, the result is as follows

        direction:
          description: actually it is lowercase string
          enum:
          - DIRECTION_UNSPECIFIED
          - FORWARD
          - BACKWARD
          - LEFT
          - RIGHT
          format: enum
          type: string

@adolfo
Copy link
Contributor

adolfo commented Mar 28, 2022

You can pass the following option to protoc to enable string-based enums:

--openapi_opt=enum_type=string

@jeffsawatzky
Copy link
Contributor

@Hangzhi / @timburks I think this issue can be closed now.

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

No branches or pull requests

4 participants