Skip to content

Commit

Permalink
add support for "title" tag
Browse files Browse the repository at this point in the history
feat(field_parser.go): add support for "title" tag in structField struct to allow specifying a custom field title
  chore(operation.go): add "titleTag" constant to define the tag name for the "title" tag used in field_parser.go to avoid hardcoding it multiple times
  feat(field_parser_test.go): test case for "title" tag support in structField struct and schema customization based on the tag value
  • Loading branch information
matteobassan committed Feb 15, 2024
1 parent 56fde5c commit a86b4e3
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 a86b4e3

Please sign in to comment.