Skip to content

Commit

Permalink
fix: Explicitly use strings for boolean-named enums
Browse files Browse the repository at this point in the history
use yaml type tag `!!str` to represent boolean-like and `NULL` enum values.

fixes: google#407
  • Loading branch information
zaakn committed Nov 9, 2023
1 parent ee84fd2 commit 2d36742
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion cmd/protoc-gen-openapi/generator/wellknown/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package wellknown

import (
"strings"

v3 "github.com/google/gnostic/openapiv3"
"google.golang.org/protobuf/reflect/protoreflect"
)
Expand Down Expand Up @@ -53,11 +55,22 @@ func NewNumberSchema(format string) *v3.SchemaOrReference {
func NewEnumSchema(enum_type *string, field protoreflect.FieldDescriptor) *v3.SchemaOrReference {
schema := &v3.Schema{Format: "enum"}
if enum_type != nil && *enum_type == "string" {
cast := func(w string) string {
reservedWords := []string{
"Y", "N", "YES", "NO", "ON", "OFF", "TRUE", "FALSE", "NULL",
}
for _, rw := range reservedWords {
if strings.EqualFold(rw, w) {
return "!!str " + w
}
}
return w
}
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()),
Yaml: cast(string(field.Enum().Values().Get(i).Name())),
})
}
} else {
Expand Down

0 comments on commit 2d36742

Please sign in to comment.