From ca2846037fcb288d3e84b7bc15aa3e4249c0f75b Mon Sep 17 00:00:00 2001 From: ElvinChan Date: Sat, 22 Sep 2018 09:52:13 +0800 Subject: [PATCH] #11 Remove `collect` swagger tag options --- README.md | 1 - README_zh-CN.md | 1 - tag.go | 14 -------------- tag_test.go | 11 ++++++----- 4 files changed, 6 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index c20d244..75ca2e6 100644 --- a/README.md +++ b/README.md @@ -212,7 +212,6 @@ The definition is equivalent to: Tag | Type | Description ---|:---:|--- -collect | `string` | Determines the format of the array if type array is used. Possible values are: Default value is `csv`. desc | `string` | Description. maximum | `number` | - minimum | `number` | - diff --git a/README_zh-CN.md b/README_zh-CN.md index cbd3f5e..15ff562 100644 --- a/README_zh-CN.md +++ b/README_zh-CN.md @@ -212,7 +212,6 @@ a.AddParamBody(&User{}, "Body", "", true) Tag | Type | Description ---|:---:|--- -collect | `string` | 如果类型是数组,确定其格式。可能的值有:默认值是 `csv`。 desc | `string` | 描述。 maximum | `number` | - minimum | `number` | - diff --git a/tag.go b/tag.go index 3cd07d0..79de6a6 100644 --- a/tag.go +++ b/tag.go @@ -54,10 +54,6 @@ func getFieldName(f reflect.StructField, in ParamInType) string { func (p *Parameter) handleSwaggerTags(field reflect.StructField, name string, in ParamInType) { tags := getSwaggerTags(field) - var collect string - if t, ok := tags["collect"]; ok && contains([]string{"csv", "ssv", "tsv", "pipes"}, t) { - collect = t - } if t, ok := tags["desc"]; ok { p.Description = t } @@ -111,7 +107,6 @@ func (p *Parameter) handleSwaggerTags(field reflect.StructField, name string, in // Move part of tags in Parameter to Items if p.Type == "array" { items := p.Items.latest() - items.CollectionFormat = collect items.Minimum = p.Minimum items.Maximum = p.Maximum items.MinLength = p.MinLength @@ -124,8 +119,6 @@ func (p *Parameter) handleSwaggerTags(field reflect.StructField, name string, in p.MaxLength = nil p.Enum = nil p.Default = nil - } else { - p.CollectionFormat = collect } } @@ -204,10 +197,6 @@ func (s *JSONSchema) handleSwaggerTags(f reflect.StructField, name string) { func (h *Header) handleSwaggerTags(f reflect.StructField, name string) { tags := getSwaggerTags(f) - var collect string - if t, ok := tags["collect"]; ok && contains([]string{"csv", "ssv", "tsv", "pipes"}, t) { - collect = t - } if t, ok := tags["desc"]; ok { h.Description = t } @@ -255,7 +244,6 @@ func (h *Header) handleSwaggerTags(f reflect.StructField, name string) { // Move part of tags in Header to Items if h.Type == "array" { items := h.Items.latest() - items.CollectionFormat = collect items.Minimum = h.Minimum items.Maximum = h.Maximum items.MinLength = h.MinLength @@ -268,8 +256,6 @@ func (h *Header) handleSwaggerTags(f reflect.StructField, name string) { h.MaxLength = nil h.Enum = nil h.Default = nil - } else { - h.CollectionFormat = collect } } diff --git a/tag_test.go b/tag_test.go index 095ea8a..1650a6f 100644 --- a/tag_test.go +++ b/tag_test.go @@ -55,7 +55,7 @@ func TestSchemaSwaggerTags(t *testing.T) { func TestParamSwaggerTags(t *testing.T) { type SearchInput struct { Q string `query:"q" swagger:"minLen(5),maxLen(8)"` - BrandIds string `query:"brandIds" swagger:"collect(csv),allowEmpty"` + BrandIds string `query:"brandIds" swagger:"allowEmpty"` Sortby [][]string `query:"sortby" swagger:"default(id),allowEmpty"` Order []int `query:"order" swagger:"enum(0|1|n)"` SkipCount int `query:"skipCount" swagger:"min(0),max(999)"` @@ -68,11 +68,12 @@ func TestParamSwaggerTags(t *testing.T) { assert.Len(t, o.Parameters, 6) assert.Equal(t, *o.Parameters[0].MinLength, 5) assert.Equal(t, *o.Parameters[0].MaxLength, 8) - assert.Equal(t, o.Parameters[1].CollectionFormat, "csv") assert.Equal(t, o.Parameters[1].AllowEmptyValue, true) assert.Equal(t, o.Parameters[2].AllowEmptyValue, true) assert.Equal(t, o.Parameters[2].Items.Items.Default, "id") + assert.Equal(t, o.Parameters[2].Items.CollectionFormat, "multi") assert.ElementsMatch(t, o.Parameters[3].Items.Enum, []int{0, 1}) + assert.Equal(t, o.Parameters[3].CollectionFormat, "multi") assert.Equal(t, *o.Parameters[4].Minimum, float64(0)) assert.Equal(t, *o.Parameters[4].Maximum, float64(999)) assert.Equal(t, o.Parameters[5].Description, "items count in one page") @@ -82,7 +83,6 @@ func TestHeaderSwaggerTags(t *testing.T) { type SearchInput struct { Q string `json:"q" swagger:"minLen(5),maxLen(8)"` Enable bool `json:"-"` - BrandIds string `json:"brandIds" swagger:"collect(csv)"` Sortby [][]string `json:"sortby" swagger:"default(id)"` Order []int `json:"order" swagger:"enum(0|1|n)"` SkipCount int `json:"skipCount" swagger:"min(0),max(999)"` @@ -94,12 +94,13 @@ func TestHeaderSwaggerTags(t *testing.T) { o := a.(*api).operation c := strconv.Itoa(http.StatusOK) h := o.Responses[c].Headers - assert.Len(t, h, 6) + assert.Len(t, h, 5) assert.Equal(t, *h["q"].MinLength, 5) assert.Equal(t, *h["q"].MaxLength, 8) - assert.Equal(t, h["brandIds"].CollectionFormat, "csv") assert.Equal(t, h["sortby"].Items.Items.Default, "id") + assert.Equal(t, h["sortby"].Items.CollectionFormat, "multi") assert.ElementsMatch(t, h["order"].Items.Enum, []int{0, 1}) + assert.Equal(t, h["order"].CollectionFormat, "multi") assert.Equal(t, *h["skipCount"].Minimum, float64(0)) assert.Equal(t, *h["skipCount"].Maximum, float64(999)) assert.Equal(t, h["maxResultCount"].Description, "items count in one page")