Skip to content

Commit

Permalink
Update api spec (#75)
Browse files Browse the repository at this point in the history
* YOYO NEW API SPEC!

* fixups

Signed-off-by: Jess Frazelle <[email protected]>

* fixups

Signed-off-by: Jess Frazelle <[email protected]>

* I have generated the latest API!

* bump version

Signed-off-by: Jess Frazelle <[email protected]>

---------

Signed-off-by: Jess Frazelle <[email protected]>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
jessfraz and github-actions[bot] authored May 2, 2023
1 parent 42f015c commit a3c6ef8
Show file tree
Hide file tree
Showing 10 changed files with 6,520 additions and 2,108 deletions.
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v0.2.13
v0.2.14
10 changes: 5 additions & 5 deletions client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/tmpl/enum.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type {{.Name}} string

const (
{{range .Values -}}
// {{.Name}} represents the {{$.Name}} `"{{.Value}}"`.
{{if .Description}}// {{.Description}}{{else}}// {{.Name}} represents the {{$.Name}} `"{{.Value}}"`.{{end}}
{{.Name}} {{$.Name}} = "{{.Value}}"
{{end -}}
)
86 changes: 79 additions & 7 deletions cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (data *Data) generateSchemaType(name string, s *openapi3.Schema, spec *open
if otype == "string" {
// If this is an enum, write the enum type.
if len(s.Enum) > 0 {
if err := data.generateEnumType(name, s); err != nil {
if err := data.generateEnumType(name, s, map[string]string{}); err != nil {
return err
}
}
Expand Down Expand Up @@ -128,11 +128,12 @@ type Enum struct {

// EnumValue holds the information for an enum value.
type EnumValue struct {
Name string
Value string
Name string
Description string
Value string
}

func (data *Data) generateEnumType(name string, s *openapi3.Schema) error {
func (data *Data) generateEnumType(name string, s *openapi3.Schema, additionalDocs map[string]string) error {
enumName := makeSingular(name)
enum := Enum{
Name: enumName,
Expand All @@ -155,10 +156,18 @@ func (data *Data) generateEnumType(name string, s *openapi3.Schema) error {
enumValueName = fmt.Sprintf("%sEmpty", enumValueName)
}

enum.Values = append(enum.Values, EnumValue{
enumValueStruct := EnumValue{
Name: enumValueName,
Value: enumValue,
})
}

if docs, ok := additionalDocs[enumValue]; ok {
if docs != "" {
enumValueStruct.Description = fmt.Sprintf("%s: %s", enumValueName, strings.ReplaceAll(docs, "\n", "\n// "))
}
}

enum.Values = append(enum.Values, enumValueStruct)
}

// Print the template for the enum.
Expand Down Expand Up @@ -256,6 +265,30 @@ func (data *Data) generateObjectType(name string, s *openapi3.Schema, spec *open
}

func (data *Data) generateOneOfType(name string, s *openapi3.Schema, spec *openapi3.T) error {
// Check if this is an enum with descriptions.
isEnumWithDocs := false
enumDocs := map[string]string{}
enumeration := []interface{}{}
for _, oneOf := range s.OneOf {
if oneOf.Value.Type == "string" && oneOf.Value.Enum != nil && len(oneOf.Value.Enum) == 1 {
// Get the description for this enum.
isEnumWithDocs = true
enumDocs[oneOf.Value.Enum[0].(string)] = oneOf.Value.Description
enumeration = append(enumeration, oneOf.Value.Enum[0])
} else {
isEnumWithDocs = false
break
}
}

if isEnumWithDocs {
return data.generateEnumType(name, &openapi3.Schema{
Type: "string",
Description: s.Description,
Enum: enumeration,
}, enumDocs)
}

// Check if they all have a type.
types := []string{}
typeName := ""
Expand Down Expand Up @@ -366,6 +399,41 @@ func isTypeToString(s string) bool {
return s == "URL" || s == "UUID" || s == "IP" || s == "Time"
}

func printOneOf(property string, r *openapi3.SchemaRef, spec *openapi3.T) (string, error) {
s := r.Value

// Check if this is an enum with descriptions.
isEnumWithDocs := false
enumeration := []interface{}{}
for _, oneOf := range s.OneOf {
if oneOf.Value.Type == "string" && oneOf.Value.Enum != nil && len(oneOf.Value.Enum) == 1 {
// Get the description for this enum.
isEnumWithDocs = true
enumeration = append(enumeration, oneOf.Value.Enum[0])
} else {
isEnumWithDocs = false
break
}
}

if isEnumWithDocs {
newSchema := &openapi3.SchemaRef{
Ref: r.Ref,
Value: &openapi3.Schema{
Type: "string",
Description: s.Description,
Enum: enumeration,
}}

if r.Ref != "" {
return getReferenceSchema(newSchema), nil
}
return printType(property, newSchema, spec)
}

return "any", nil
}

// printType converts a schema type to a valid Go type.
func printType(property string, r *openapi3.SchemaRef, spec *openapi3.T) (string, error) {
s := r.Value
Expand All @@ -383,7 +451,7 @@ func printType(property string, r *openapi3.SchemaRef, spec *openapi3.T) (string
// If the reference is an object or an enum, return the reference.
// If we have a oneOf we are going to use a generic for it.
if reference.Value.OneOf != nil {
return "any", nil
return printOneOf(property, r, spec)
} else if reference.Value.Type == "object" || reference.Value.Type == "string" && len(reference.Value.Enum) > 0 {
return getReferenceSchema(r), nil
}
Expand All @@ -401,6 +469,10 @@ func printType(property string, r *openapi3.SchemaRef, spec *openapi3.T) (string
return printType(property, s.AllOf[0], spec)
}

if s.OneOf != nil {
return printOneOf(property, r, spec)
}

if t == "string" {
reference := getReferenceSchema(r)
if reference != "" {
Expand Down
Loading

0 comments on commit a3c6ef8

Please sign in to comment.