Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions internal/test/components/components.gen.go

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

4 changes: 2 additions & 2 deletions internal/test/schemas/schemas.gen.go

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

1 change: 1 addition & 0 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,7 @@ func GenerateParamsTypes(op OperationDefinition) []TypeDefinition {
s.Properties = append(s.Properties, prop)
}

s.Description = op.Spec.Description
s.GoType = GenStructFromSchema(s)

td := TypeDefinition{
Expand Down
33 changes: 18 additions & 15 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ type Schema struct {
AdditionalTypes []TypeDefinition // We may need to generate auxiliary helper types, stored here

SkipOptionalPointer bool // Some types don't need a * in front when they're optional

Description string // The description of the element
}

func (s Schema) IsRef() bool {
Expand Down Expand Up @@ -98,39 +100,43 @@ func PropertiesEqual(a, b Property) bool {
}

func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
// If Ref is set on the SchemaRef, it means that this type is actually a reference to
// another type. We're not de-referencing, so simply use the referenced type.
var refType string

// Add a fallback value in case the sref is nil.
// i.e. the parent schema defines a type:array, but the array has
// no items defined. Therefore we have at least valid Go-Code.
if sref == nil {
return Schema{GoType: "interface{}", RefType: refType}, nil
return Schema{GoType: "interface{}"}, nil
}

schema := sref.Value

// If Ref is set on the SchemaRef, it means that this type is actually a reference to
// another type. We're not de-referencing, so simply use the referenced type.
if IsGoTypeReference(sref.Ref) {
var err error
// Convert the reference path to Go type
refType, err = RefPathToGoType(sref.Ref)
refType, err := RefPathToGoType(sref.Ref)
if err != nil {
return Schema{}, fmt.Errorf("error turning reference (%s) into a Go type: %s",
sref.Ref, err)
}
return Schema{
GoType: refType,
GoType: refType,
Description: schema.Description,
}, nil
}

outSchema := Schema{
Description: schema.Description,
}

// We can't support this in any meaningful way
if schema.AnyOf != nil {
return Schema{GoType: "interface{}", RefType: refType}, nil
outSchema.GoType = "interface{}"
return outSchema, nil
}
// We can't support this in any meaningful way
if schema.OneOf != nil {
return Schema{GoType: "interface{}", RefType: refType}, nil
outSchema.GoType = "interface{}"
return outSchema, nil
}

// AllOf is interesting, and useful. It's the union of a number of other
Expand All @@ -142,14 +148,9 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
if err != nil {
return Schema{}, errors.Wrap(err, "error merging schemas")
}
mergedSchema.RefType = refType
return mergedSchema, nil
}

outSchema := Schema{
RefType: refType,
}

// Check for custom Go type extension
if extension, ok := schema.Extensions[extPropGoType]; ok {
typeName, err := extTypeName(extension)
Expand Down Expand Up @@ -493,6 +494,7 @@ func paramToGoType(param *openapi3.Parameter, path []string) (Schema, error) {
if len(param.Content) > 1 {
return Schema{
GoType: "string",
Description: param.Description,
}, nil
}

Expand All @@ -502,6 +504,7 @@ func paramToGoType(param *openapi3.Parameter, path []string) (Schema, error) {
// If we don't have json, it's a string
return Schema{
GoType: "string",
Description: param.Description,
}, nil
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/codegen/templates/templates.gen.go

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

2 changes: 1 addition & 1 deletion pkg/codegen/templates/typedef.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{range .Types}}
// {{.TypeName}} defines model for {{.JsonName}}.
// {{ with .Schema.Description }}{{ . }}{{ else }}{{.TypeName}} defines model for {{.JsonName}}.{{ end }}
type {{.TypeName}} {{if and (opts.AliasTypes) (.CanAlias)}}={{end}} {{.Schema.TypeDecl}}
{{- if gt (len .Schema.EnumValues) 0 }}
// List of {{ .TypeName }}
Expand Down