Skip to content

Conversation

@adolfo
Copy link
Contributor

@adolfo adolfo commented Feb 19, 2022

This change exposes a configuration option that enables Protobuf enums to be compiled to enums with the underlying type of string.

The following Protobuf message:

message Message {
  Kind kind = 1;
}

enum Kind {
  KIND_UNSPECIFIED = 0;
  KIND_1 = 1;
  KIND_2 = 2;
}

Will be compiled to the following OpenAPI spec:

Message:
  type: object
  properties:
      kind:
          format: enum
          type: string
          enum:
              - KIND_UNSPECIFIED
              - KIND_1
              - KIND_2

And will be compiled to the following jsonschema spec:

{
  "title": "Message",
  "$id": "http://example.com/schemas/Message.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "kind": {
      "title": "kind",
      "type": "string",
      "enum": [
        "KIND_UNSPECIFIED",
        "KIND_1",
        "KIND_2"
      ],
      "format": "enum"
    }
  }
}

To enable string serialization, pass the enum_type flag:

$ protoc sample.proto -I. --openapi_out=enum_type=string:.
$ protoc sample.proto -I. --jsonschema_out=enum_type=string:.

The existing enum behavior remains unchanged and defaults to integer when the enum_type flag is not provided.

@adolfo adolfo requested a review from a team as a code owner February 19, 2022 04:25
@adolfo adolfo changed the title Add support for string-based enums protoc-gen-openapi: Add support for string-based enums Feb 19, 2022
@adolfo adolfo force-pushed the enum-serialization-config branch from a748139 to fb8161f Compare February 19, 2022 06:15
@adolfo adolfo changed the title protoc-gen-openapi: Add support for string-based enums protoc-gen-openapi/jsonschema: Add support for string-based enums Feb 19, 2022
@morphar
Copy link
Collaborator

morphar commented Feb 19, 2022

Nice! Thank you :)
Do you know if Envoy would accept this form? Or is this meant for another JSON transcoder?

LGTM @timburks

@adolfo
Copy link
Contributor Author

adolfo commented Feb 19, 2022

@morphar Yes, Envoy will pass these string enum values through to the gRPC service and even validate them. You'll need the always_print_enums_as_ints: false option for Envoy to return a string value in the response.

I'm also using the compiled OpenAPI spec to generate a web client (using openapi-generator) that uses these enum values to create language-specific enum values (Elm). Working well so far.

@morphar
Copy link
Collaborator

morphar commented Feb 19, 2022

@adolfo Very nice! :)
I really like the idea of using strings instead of ints in the REST layer. I will definitely give this a try in my own setup.

Copy link
Contributor

@timburks timburks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deferring to @morphar's LGTM - thanks for adding this!

@timburks timburks merged commit a03001a into google:master Mar 14, 2022
dodangquan pushed a commit to dodangquan/protoc-gen-openapi that referenced this pull request Oct 3, 2025
…ogle#312)

* protoc-gen-openapi: Expose configuration for enum serialization type
* protoc-gen-jsonschema: Expose configuration for enum serialization type
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 this pull request may close these issues.

3 participants