Skip to content

Commit

Permalink
add support for "title" tag (#1762)
Browse files Browse the repository at this point in the history
feat: add support for "title" tag in structField struct to allow specifying a custom field title
  • Loading branch information
matteobassan committed Feb 20, 2024
1 parent 87e7d9c commit 91624ad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
3 changes: 3 additions & 0 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (ps *tagBaseFieldParser) CustomSchema() (*spec.Schema, error) {
}

type structField struct {
title string
schemaType string
arrayType string
formatType string
Expand Down Expand Up @@ -274,6 +275,7 @@ func (ps *tagBaseFieldParser) complementSchema(schema *spec.Schema, types []stri
field := &structField{
schemaType: types[0],
formatType: ps.tag.Get(formatTag),
title: ps.tag.Get(titleTag),
}

if len(types) > 1 && (types[0] == ARRAY || types[0] == OBJECT) {
Expand Down Expand Up @@ -414,6 +416,7 @@ func (ps *tagBaseFieldParser) complementSchema(schema *spec.Schema, types []stri
if field.schemaType != ARRAY {
schema.Format = field.formatType
}
schema.Title = field.title

extensionsTagValue := ps.tag.Get(extensionsTag)
if extensionsTagValue != "" {
Expand Down
15 changes: 15 additions & 0 deletions field_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,21 @@ func TestDefaultFieldParser(t *testing.T) {
assert.Equal(t, "csv", schema.Format)
})

t.Run("Title tag", func(t *testing.T) {
t.Parallel()

schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" title:"myfield"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, "myfield", schema.Title)
})

t.Run("Required tag", func(t *testing.T) {
t.Parallel()

Expand Down
1 change: 1 addition & 0 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ const (
exampleTag = "example"
schemaExampleTag = "schemaExample"
formatTag = "format"
titleTag = "title"
validateTag = "validate"
minimumTag = "minimum"
maximumTag = "maximum"
Expand Down

0 comments on commit 91624ad

Please sign in to comment.