Skip to content

Commit

Permalink
Correctly parse format on array fields (#973)
Browse files Browse the repository at this point in the history
When the format directive is used on an array type the indicated format
should go under the items key.
  • Loading branch information
j-sv committed Aug 17, 2021
1 parent 485f917 commit 497e6e2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 13 deletions.
5 changes: 4 additions & 1 deletion parser.go
Expand Up @@ -1034,11 +1034,14 @@ func (parser *Parser) parseStructField(file *ast.File, field *ast.Field) (map[st
schema.ReadOnly = structField.readOnly
schema.Default = structField.defaultValue
schema.Example = structField.exampleValue
schema.Format = structField.formatType
if structField.schemaType != ARRAY {
schema.Format = structField.formatType
}
schema.Extensions = structField.extensions
eleSchema := schema
if structField.schemaType == ARRAY {
eleSchema = schema.Items.Schema
eleSchema.Format = structField.formatType
}
eleSchema.Maximum = structField.maximum
eleSchema.Minimum = structField.minimum
Expand Down
16 changes: 12 additions & 4 deletions parser_test.go
Expand Up @@ -764,9 +764,9 @@ func TestParseSimpleApi_ForSnakecase(t *testing.T) {
},
"photo_urls": {
"type": "array",
"format": "url",
"items": {
"type": "string"
"type": "string",
"format": "url"
},
"example": [
"http://test/image/1.jpg",
Expand Down Expand Up @@ -1240,9 +1240,9 @@ func TestParseSimpleApi_ForLowerCamelcase(t *testing.T) {
},
"photoURLs": {
"type": "array",
"format": "url",
"items": {
"type": "string"
"type": "string",
"format": "url"
},
"example": [
"http://test/image/1.jpg",
Expand Down Expand Up @@ -2718,6 +2718,14 @@ func TestParseJSONFieldString(t *testing.T) {
"description": "boolean as a string",
"type": "string",
"example": "true"
},
"uuids": {
"description": "string array with format",
"type": "array",
"items": {
"type": "string",
"format": "uuid"
}
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions testdata/json_field_string/main.go
Expand Up @@ -7,12 +7,13 @@ import (
)

type MyStruct struct {
ID int `json:"id" example:"1" format:"int64"`
Name string `json:"name" example:"poti"`
Intvar int `json:"myint,string"` // integer as string
Boolvar bool `json:",string"` // boolean as a string
TrueBool bool `json:"truebool,string" example:"true"` // boolean as a string
Floatvar float64 `json:",string"` // float as a string
ID int `json:"id" example:"1" format:"int64"`
Name string `json:"name" example:"poti"`
Intvar int `json:"myint,string"` // integer as string
Boolvar bool `json:",string"` // boolean as a string
TrueBool bool `json:"truebool,string" example:"true"` // boolean as a string
Floatvar float64 `json:",string"` // float as a string
UUIDs []string `json:"uuids" type:"array,string" format:"uuid"` // string array with format
}

// @Summary Call DoSomething
Expand Down
4 changes: 2 additions & 2 deletions testdata/simple/expected.json
Expand Up @@ -449,9 +449,9 @@
},
"photo_urls": {
"type": "array",
"format": "url",
"items": {
"type": "string"
"type": "string",
"format": "url"
},
"example": [
"http://test/image/1.jpg",
Expand Down

0 comments on commit 497e6e2

Please sign in to comment.