gnostic icon indicating copy to clipboard operation
gnostic copied to clipboard

Generate enum with values when using protoc-gen-openapi

Open Hangzhi opened this issue 4 years ago • 3 comments

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?

Hangzhi avatar Aug 12 '21 09:08 Hangzhi

https://github.com/google/gnostic/blob/418d86c152e3f607fa625e9aca135091e574811f/cmd/protoc-gen-openapi/main.go#L34

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

renyijiu avatar Mar 28 '22 07:03 renyijiu

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

--openapi_opt=enum_type=string

adolfo avatar Mar 28 '22 14:03 adolfo

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

jeffsawatzky avatar Apr 05 '22 03:04 jeffsawatzky