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
Binary file added cmd/oapi-codegen/oapi-codegen
Binary file not shown.
18 changes: 17 additions & 1 deletion pkg/codegen/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
)

const (
extPropGoType = "x-go-type"
extPropGoType = "x-go-type"
extPropOmitEmpty = "x-omitempty"
)

func extTypeName(extPropValue interface{}) (string, error) {
Expand All @@ -23,3 +24,18 @@ func extTypeName(extPropValue interface{}) (string, error) {

return name, nil
}

func extParseOmitEmpty(extPropValue interface{}) (bool, error) {

raw, ok := extPropValue.(json.RawMessage)
if !ok {
return false, fmt.Errorf("failed to convert type: %T", extPropValue)
}

var omitEmpty bool
if err := json.Unmarshal(raw, &omitEmpty); err != nil {
return false, errors.Wrap(err, "failed to unmarshal json")
}

return omitEmpty, nil
}
9 changes: 5 additions & 4 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,10 +596,11 @@ func GenerateParamsTypes(op OperationDefinition) []TypeDefinition {
})
}
prop := Property{
Description: param.Spec.Description,
JsonFieldName: param.ParamName,
Required: param.Required,
Schema: pSchema,
Description: param.Spec.Description,
JsonFieldName: param.ParamName,
Required: param.Required,
Schema: pSchema,
ExtensionProps: &param.Spec.ExtensionProps,
}
s.Properties = append(s.Properties, prop)
}
Expand Down
33 changes: 22 additions & 11 deletions pkg/codegen/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ func (s Schema) GetAdditionalTypeDefs() []TypeDefinition {
}

type Property struct {
Description string
JsonFieldName string
Schema Schema
Required bool
Nullable bool
Description string
JsonFieldName string
Schema Schema
Required bool
Nullable bool
ExtensionProps *openapi3.ExtensionProps
}

func (p Property) GoFieldName() string {
Expand Down Expand Up @@ -240,11 +241,12 @@ func GenerateGoSchema(sref *openapi3.SchemaRef, path []string) (Schema, error) {
description = p.Value.Description
}
prop := Property{
JsonFieldName: pName,
Schema: pSchema,
Required: required,
Description: description,
Nullable: p.Value.Nullable,
JsonFieldName: pName,
Schema: pSchema,
Required: required,
Description: description,
Nullable: p.Value.Nullable,
ExtensionProps: &p.Value.ExtensionProps,
}
outSchema.Properties = append(outSchema.Properties, prop)
}
Expand Down Expand Up @@ -371,7 +373,16 @@ func GenFieldsFromProperties(props []Property) []string {
field += fmt.Sprintf("\n%s\n", StringToGoComment(p.Description))
}
field += fmt.Sprintf(" %s %s", p.GoFieldName(), p.GoTypeDef())
if p.Required || p.Nullable {

// Support x-omitempty
omitEmpty := true
if _, ok := p.ExtensionProps.Extensions[extPropOmitEmpty]; ok {
if extOmitEmpty, err := extParseOmitEmpty(p.ExtensionProps.Extensions[extPropOmitEmpty]); err == nil {
omitEmpty = extOmitEmpty
}
}

if p.Required || p.Nullable || !omitEmpty {
field += fmt.Sprintf(" `json:\"%s\"`", p.JsonFieldName)
} else {
field += fmt.Sprintf(" `json:\"%s,omitempty\"`", p.JsonFieldName)
Expand Down