Skip to content

Commit

Permalink
feat: struct fields allow empty example strings (#1125)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan committed Feb 3, 2022
1 parent 808bde6 commit e1ac437
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions field_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ func (ps *tagBaseFieldParser) ComplementSchema(schema *spec.Schema) error {
jsonTag := ps.tag.Get(jsonTag)
// json:"name,string" or json:",string"

exampleTag := ps.tag.Get(exampleTag)
if exampleTag != "" {
exampleTag, ok := ps.tag.Lookup(exampleTag)
if ok {
structField.exampleValue = exampleTag
if !strings.Contains(jsonTag, ",string") {
example, err := defineTypeOfExample(structField.schemaType, structField.arrayType, exampleTag)
Expand Down
11 changes: 11 additions & 0 deletions field_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ func TestDefaultFieldParser(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, "one", schema.Example)

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

schema = spec.Schema{}
schema.Type = []string{"float"}
err = newTagBaseFieldParser(
Expand Down

0 comments on commit e1ac437

Please sign in to comment.