Skip to content
Open
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
36 changes: 34 additions & 2 deletions pkg/codegen/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,44 @@ func (pd ParameterDefinition) ZeroValueIsNil() bool {
// JsonTag generates the JSON annotation to map GoType to json type name. If Parameter
// Foo is marshaled to json as "foo", this will create the annotation
// 'json:"foo"'
// It also includes any additional struct tags from x-oapi-codegen-extra-tags
// at the parameter or schema level (parameter-level takes precedence).
func (pd *ParameterDefinition) JsonTag() string {
fieldTags := make(map[string]string)

if pd.Required {
return fmt.Sprintf("`json:\"%s\"`", pd.ParamName)
fieldTags["json"] = pd.ParamName
} else {
return fmt.Sprintf("`json:\"%s,omitempty\"`", pd.ParamName)
fieldTags["json"] = pd.ParamName + ",omitempty"
}

// Merge x-oapi-codegen-extra-tags from schema level first, then parameter level
// so that parameter-level takes precedence.
if pd.Spec != nil && pd.Spec.Schema != nil && pd.Spec.Schema.Value != nil {
if extension, ok := pd.Spec.Schema.Value.Extensions[extPropExtraTags]; ok {
if tags, err := extExtraTags(extension); err == nil {
for k, v := range tags {
fieldTags[k] = v
}
}
}
}
if pd.Spec != nil {
if extension, ok := pd.Spec.Extensions[extPropExtraTags]; ok {
if tags, err := extExtraTags(extension); err == nil {
for k, v := range tags {
fieldTags[k] = v
}
}
}
}

keys := SortedMapKeys(fieldTags)
tags := make([]string, len(keys))
for i, k := range keys {
tags[i] = fmt.Sprintf(`%s:"%s"`, k, fieldTags[k])
}
return "`" + strings.Join(tags, " ") + "`"
}

func (pd *ParameterDefinition) IsJson() bool {
Expand Down
82 changes: 82 additions & 0 deletions pkg/codegen/operations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/getkin/kin-openapi/openapi3"
"github.com/stretchr/testify/assert"
)

func TestIsJson(t *testing.T) {
Expand Down Expand Up @@ -146,3 +147,84 @@ func TestGenerateDefaultOperationID(t *testing.T) {
}
}
}

func TestJsonTag(t *testing.T) {
t.Run("required param with no extra tags", func(t *testing.T) {
pd := ParameterDefinition{
ParamName: "foo",
Required: true,
Spec: &openapi3.Parameter{},
}
assert.Equal(t, "`json:\"foo\"`", pd.JsonTag())
})

t.Run("optional param with no extra tags", func(t *testing.T) {
pd := ParameterDefinition{
ParamName: "foo",
Required: false,
Spec: &openapi3.Parameter{},
}
assert.Equal(t, "`json:\"foo,omitempty\"`", pd.JsonTag())
})

t.Run("extra tags at parameter level", func(t *testing.T) {
pd := ParameterDefinition{
ParamName: "foo",
Required: true,
Spec: &openapi3.Parameter{
Extensions: map[string]any{
"x-oapi-codegen-extra-tags": map[string]any{
"validate": "required",
"db": "foo_col",
},
},
},
}
assert.Equal(t, "`db:\"foo_col\" json:\"foo\" validate:\"required\"`", pd.JsonTag())
})

t.Run("extra tags at schema level", func(t *testing.T) {
pd := ParameterDefinition{
ParamName: "foo",
Required: true,
Spec: &openapi3.Parameter{
Schema: &openapi3.SchemaRef{
Value: &openapi3.Schema{
Extensions: map[string]any{
"x-oapi-codegen-extra-tags": map[string]any{
"validate": "required",
},
},
},
},
},
}
assert.Equal(t, "`json:\"foo\" validate:\"required\"`", pd.JsonTag())
})

t.Run("parameter level takes precedence over schema level", func(t *testing.T) {
pd := ParameterDefinition{
ParamName: "foo",
Required: true,
Spec: &openapi3.Parameter{
Extensions: map[string]any{
"x-oapi-codegen-extra-tags": map[string]any{
"validate": "param-level",
},
},
Schema: &openapi3.SchemaRef{
Value: &openapi3.Schema{
Extensions: map[string]any{
"x-oapi-codegen-extra-tags": map[string]any{
"validate": "schema-level",
"db": "foo_col",
},
},
},
},
},
}
// Parameter-level "validate" wins, schema-level "db" is kept
assert.Equal(t, "`db:\"foo_col\" json:\"foo\" validate:\"param-level\"`", pd.JsonTag())
})
}
Loading