From 497e6e274a73553a71249653bc5627ba55fd4801 Mon Sep 17 00:00:00 2001 From: Johan Svan Date: Tue, 17 Aug 2021 12:31:56 +0200 Subject: [PATCH] Correctly parse format on array fields (#973) When the format directive is used on an array type the indicated format should go under the items key. --- parser.go | 5 ++++- parser_test.go | 16 ++++++++++++---- testdata/json_field_string/main.go | 13 +++++++------ testdata/simple/expected.json | 4 ++-- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/parser.go b/parser.go index eef23ca1a..10bd60b98 100644 --- a/parser.go +++ b/parser.go @@ -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 diff --git a/parser_test.go b/parser_test.go index e4bdb6feb..d2265b57b 100644 --- a/parser_test.go +++ b/parser_test.go @@ -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", @@ -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", @@ -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" + } } } } diff --git a/testdata/json_field_string/main.go b/testdata/json_field_string/main.go index 564503a2a..ce9d23726 100755 --- a/testdata/json_field_string/main.go +++ b/testdata/json_field_string/main.go @@ -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 diff --git a/testdata/simple/expected.json b/testdata/simple/expected.json index d4fcad58b..07502c3cb 100644 --- a/testdata/simple/expected.json +++ b/testdata/simple/expected.json @@ -449,9 +449,9 @@ }, "photo_urls": { "type": "array", - "format": "url", "items": { - "type": "string" + "type": "string", + "format": "url" }, "example": [ "http://test/image/1.jpg",