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-openapi: Enum types that use stringified boolean values like "TRUE" are treated as the real boolean values #407

Open
zaakn opened this issue Sep 16, 2023 · 2 comments · May be fixed by #413

Comments

@zaakn
Copy link

zaakn commented Sep 16, 2023

env

  • protoc-gen-openapi: v0.6.8
  • protoc flags: --openapi_opt naming=proto,enum_type=string

proto file

enum Trinary {
	NULL = 0;
	TRUE = 1;
	FALSE = 2;
}

message Condition {
	Trinary attached = 1;
}

expected output

Condition:
    type: object
    properties:
        attached:
            enum:
                - 'NULL'
                - 'TRUE'
                - 'FALSE'
            type: string
            format: enum

actual output

Condition:
    type: object
    properties:
        attached:
            enum:
                - NULL
                - TRUE
                - FALSE
            type: string
            format: enum

I know it's better not to use reserved words, so this is just for usage feedback.

@zaakn
Copy link
Author

zaakn commented Sep 17, 2023

Related code lines:

func NewEnumSchema(enum_type *string, field protoreflect.FieldDescriptor) *v3.SchemaOrReference {
schema := &v3.Schema{Format: "enum"}
if enum_type != nil && *enum_type == "string" {
schema.Type = "string"
schema.Enum = make([]*v3.Any, 0, field.Enum().Values().Len())
for i := 0; i < field.Enum().Values().Len(); i++ {
schema.Enum = append(schema.Enum, &v3.Any{
Yaml: string(field.Enum().Values().Get(i).Name()),
})
}
} else {

My simple solution is:

schema.Enum = append(schema.Enum, &v3.Any{
	Yaml: strconv.Quote(string(field.Enum().Values().Get(i).Name())),
})

But is it correct to add Quote here?
It has to be said that TRUE (string without quotes) will be treated as a boolean which is the expected behavior of YAML.

See also:
https://github.com/google/gnostic-models/blob/c7be7c783f49e86348a1081eb387e01a347a4f23/openapiv3/OpenAPIv3.go#L6712-L6716

@nightlyone
Copy link
Contributor

@zaakn a safer way to handle that instead of using quoting might be to set data type annotation like !!str in the schema.Enum.Yaml field and strip them again when rendering JSON. This allows to avoid adding annotations,

zaakn added a commit to zaakn/gnostic that referenced this issue Nov 9, 2023
use yaml type tag `!!str` to represent boolean-like and `NULL` enum values.

fixes: google#407
@zaakn zaakn linked a pull request Nov 9, 2023 that will close this issue
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.

2 participants