Merge pull request #12 from elvinchan/11
#11 Remove `collect` swagger tag options
This commit is contained in:
commit
41e3ba6bb9
@ -212,7 +212,6 @@ The definition is equivalent to:
|
|||||||
|
|
||||||
Tag | Type | Description
|
Tag | Type | Description
|
||||||
---|:---:|---
|
---|:---:|---
|
||||||
collect | `string` | Determines the format of the array if type array is used. Possible values are: <ul><li>`csv` - comma separated values `foo,bar`. <li>`ssv` - space separated values `foo bar`. <li>`tsv` - tab separated values `foo\tbar`. <li>`pipes` - pipe separated values `foo\|bar`. </ul> Default value is `csv`.
|
|
||||||
desc | `string` | Description.
|
desc | `string` | Description.
|
||||||
maximum | `number` | -
|
maximum | `number` | -
|
||||||
minimum | `number` | -
|
minimum | `number` | -
|
||||||
|
@ -212,7 +212,6 @@ a.AddParamBody(&User{}, "Body", "", true)
|
|||||||
|
|
||||||
Tag | Type | Description
|
Tag | Type | Description
|
||||||
---|:---:|---
|
---|:---:|---
|
||||||
collect | `string` | 如果类型是数组,确定其格式。可能的值有:<ul><li>`csv` - 逗号分隔的值 `foo,bar`。<li>`ssv` - 空格分隔的值 `foo bar`。<li>`tsv` - tab分隔的值 `foo\tbar`。<li>`pipes` - pipe分隔的值 `foo\|bar`。</ul>默认值是 `csv`。
|
|
||||||
desc | `string` | 描述。
|
desc | `string` | 描述。
|
||||||
maximum | `number` | -
|
maximum | `number` | -
|
||||||
minimum | `number` | -
|
minimum | `number` | -
|
||||||
|
14
tag.go
14
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) {
|
func (p *Parameter) handleSwaggerTags(field reflect.StructField, name string, in ParamInType) {
|
||||||
tags := getSwaggerTags(field)
|
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 {
|
if t, ok := tags["desc"]; ok {
|
||||||
p.Description = t
|
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
|
// Move part of tags in Parameter to Items
|
||||||
if p.Type == "array" {
|
if p.Type == "array" {
|
||||||
items := p.Items.latest()
|
items := p.Items.latest()
|
||||||
items.CollectionFormat = collect
|
|
||||||
items.Minimum = p.Minimum
|
items.Minimum = p.Minimum
|
||||||
items.Maximum = p.Maximum
|
items.Maximum = p.Maximum
|
||||||
items.MinLength = p.MinLength
|
items.MinLength = p.MinLength
|
||||||
@ -124,8 +119,6 @@ func (p *Parameter) handleSwaggerTags(field reflect.StructField, name string, in
|
|||||||
p.MaxLength = nil
|
p.MaxLength = nil
|
||||||
p.Enum = nil
|
p.Enum = nil
|
||||||
p.Default = 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) {
|
func (h *Header) handleSwaggerTags(f reflect.StructField, name string) {
|
||||||
tags := getSwaggerTags(f)
|
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 {
|
if t, ok := tags["desc"]; ok {
|
||||||
h.Description = t
|
h.Description = t
|
||||||
}
|
}
|
||||||
@ -255,7 +244,6 @@ func (h *Header) handleSwaggerTags(f reflect.StructField, name string) {
|
|||||||
// Move part of tags in Header to Items
|
// Move part of tags in Header to Items
|
||||||
if h.Type == "array" {
|
if h.Type == "array" {
|
||||||
items := h.Items.latest()
|
items := h.Items.latest()
|
||||||
items.CollectionFormat = collect
|
|
||||||
items.Minimum = h.Minimum
|
items.Minimum = h.Minimum
|
||||||
items.Maximum = h.Maximum
|
items.Maximum = h.Maximum
|
||||||
items.MinLength = h.MinLength
|
items.MinLength = h.MinLength
|
||||||
@ -268,8 +256,6 @@ func (h *Header) handleSwaggerTags(f reflect.StructField, name string) {
|
|||||||
h.MaxLength = nil
|
h.MaxLength = nil
|
||||||
h.Enum = nil
|
h.Enum = nil
|
||||||
h.Default = nil
|
h.Default = nil
|
||||||
} else {
|
|
||||||
h.CollectionFormat = collect
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
11
tag_test.go
11
tag_test.go
@ -55,7 +55,7 @@ func TestSchemaSwaggerTags(t *testing.T) {
|
|||||||
func TestParamSwaggerTags(t *testing.T) {
|
func TestParamSwaggerTags(t *testing.T) {
|
||||||
type SearchInput struct {
|
type SearchInput struct {
|
||||||
Q string `query:"q" swagger:"minLen(5),maxLen(8)"`
|
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"`
|
Sortby [][]string `query:"sortby" swagger:"default(id),allowEmpty"`
|
||||||
Order []int `query:"order" swagger:"enum(0|1|n)"`
|
Order []int `query:"order" swagger:"enum(0|1|n)"`
|
||||||
SkipCount int `query:"skipCount" swagger:"min(0),max(999)"`
|
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.Len(t, o.Parameters, 6)
|
||||||
assert.Equal(t, *o.Parameters[0].MinLength, 5)
|
assert.Equal(t, *o.Parameters[0].MinLength, 5)
|
||||||
assert.Equal(t, *o.Parameters[0].MaxLength, 8)
|
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[1].AllowEmptyValue, true)
|
||||||
assert.Equal(t, o.Parameters[2].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.Items.Default, "id")
|
||||||
|
assert.Equal(t, o.Parameters[2].Items.CollectionFormat, "multi")
|
||||||
assert.ElementsMatch(t, o.Parameters[3].Items.Enum, []int{0, 1})
|
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].Minimum, float64(0))
|
||||||
assert.Equal(t, *o.Parameters[4].Maximum, float64(999))
|
assert.Equal(t, *o.Parameters[4].Maximum, float64(999))
|
||||||
assert.Equal(t, o.Parameters[5].Description, "items count in one page")
|
assert.Equal(t, o.Parameters[5].Description, "items count in one page")
|
||||||
@ -82,7 +83,6 @@ func TestHeaderSwaggerTags(t *testing.T) {
|
|||||||
type SearchInput struct {
|
type SearchInput struct {
|
||||||
Q string `json:"q" swagger:"minLen(5),maxLen(8)"`
|
Q string `json:"q" swagger:"minLen(5),maxLen(8)"`
|
||||||
Enable bool `json:"-"`
|
Enable bool `json:"-"`
|
||||||
BrandIds string `json:"brandIds" swagger:"collect(csv)"`
|
|
||||||
Sortby [][]string `json:"sortby" swagger:"default(id)"`
|
Sortby [][]string `json:"sortby" swagger:"default(id)"`
|
||||||
Order []int `json:"order" swagger:"enum(0|1|n)"`
|
Order []int `json:"order" swagger:"enum(0|1|n)"`
|
||||||
SkipCount int `json:"skipCount" swagger:"min(0),max(999)"`
|
SkipCount int `json:"skipCount" swagger:"min(0),max(999)"`
|
||||||
@ -94,12 +94,13 @@ func TestHeaderSwaggerTags(t *testing.T) {
|
|||||||
o := a.(*api).operation
|
o := a.(*api).operation
|
||||||
c := strconv.Itoa(http.StatusOK)
|
c := strconv.Itoa(http.StatusOK)
|
||||||
h := o.Responses[c].Headers
|
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"].MinLength, 5)
|
||||||
assert.Equal(t, *h["q"].MaxLength, 8)
|
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.Items.Default, "id")
|
||||||
|
assert.Equal(t, h["sortby"].Items.CollectionFormat, "multi")
|
||||||
assert.ElementsMatch(t, h["order"].Items.Enum, []int{0, 1})
|
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"].Minimum, float64(0))
|
||||||
assert.Equal(t, *h["skipCount"].Maximum, float64(999))
|
assert.Equal(t, *h["skipCount"].Maximum, float64(999))
|
||||||
assert.Equal(t, h["maxResultCount"].Description, "items count in one page")
|
assert.Equal(t, h["maxResultCount"].Description, "items count in one page")
|
||||||
|
Loading…
Reference in New Issue
Block a user