1 Star 0 Fork 0

brucelin_cn/swag

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
field_parser_test.go 13.77 KB
一键复制 编辑 原始数据 按行查看 历史
coldWater 提交于 2021-11-12 02:23 . chore: fix bug and refactor (#1048)
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
package swag
import (
"go/ast"
"testing"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
)
func TestDefaultFieldParser(t *testing.T) {
t.Run("Example tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" example:"one"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, "one", schema.Example)
schema = spec.Schema{}
schema.Type = []string{"float"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" example:"one"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
})
t.Run("Format tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" format:"csv"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, "csv", schema.Format)
})
t.Run("Required tag", func(t *testing.T) {
t.Parallel()
got, err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" binding:"required"`,
}},
).IsRequired()
assert.NoError(t, err)
assert.Equal(t, true, got)
got, err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required"`,
}},
).IsRequired()
assert.NoError(t, err)
assert.Equal(t, true, got)
})
t.Run("Extensions tag", func(t *testing.T) {
})
t.Run("Enums tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" enums:"a,b,c"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{"a", "b", "c"}, schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"float"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" enums:"a,b,c"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
})
t.Run("Default tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" default:"pass"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, "pass", schema.Default)
schema = spec.Schema{}
schema.Type = []string{"float"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" default:"pass"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
})
t.Run("Numeric value", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"integer"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" maximum:"1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
max := float64(1)
assert.Equal(t, &max, schema.Maximum)
schema = spec.Schema{}
schema.Type = []string{"integer"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" maximum:"one"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
schema = spec.Schema{}
schema.Type = []string{"number"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" maximum:"1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
max = float64(1)
assert.Equal(t, &max, schema.Maximum)
schema = spec.Schema{}
schema.Type = []string{"number"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" maximum:"one"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
schema = spec.Schema{}
schema.Type = []string{"number"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" multipleOf:"1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
multipleOf := float64(1)
assert.Equal(t, &multipleOf, schema.MultipleOf)
schema = spec.Schema{}
schema.Type = []string{"number"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" multipleOf:"one"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
schema = spec.Schema{}
schema.Type = []string{"integer"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" minimum:"1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
min := float64(1)
assert.Equal(t, &min, schema.Minimum)
schema = spec.Schema{}
schema.Type = []string{"integer"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" minimum:"one"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
})
t.Run("String value", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" maxLength:"1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
max := int64(1)
assert.Equal(t, &max, schema.MaxLength)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" maxLength:"one"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" minLength:"1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
min := int64(1)
assert.Equal(t, &min, schema.MinLength)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" minLength:"one"`,
}},
).ComplementSchema(&schema)
assert.Error(t, err)
})
t.Run("Readonly tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" readonly:"true"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, true, schema.ReadOnly)
})
t.Run("Invalid tag", func(t *testing.T) {
t.Parallel()
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Names: []*ast.Ident{{Name: "BasicStruct"}}},
).ComplementSchema(nil)
assert.Error(t, err)
})
}
func TestValidTags(t *testing.T) {
t.Run("Required with max/min tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,max=10,min=1"`,
}},
).ComplementSchema(&schema)
max := int64(10)
min := int64(1)
assert.NoError(t, err)
assert.Equal(t, &max, schema.MaxLength)
assert.Equal(t, &min, schema.MinLength)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,max=10,gte=1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, &max, schema.MaxLength)
assert.Equal(t, &min, schema.MinLength)
schema = spec.Schema{}
schema.Type = []string{"integer"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,max=10,min=1"`,
}},
).ComplementSchema(&schema)
maxFloat64 := float64(10)
minFloat64 := float64(1)
assert.NoError(t, err)
assert.Equal(t, &maxFloat64, schema.Maximum)
assert.Equal(t, &minFloat64, schema.Minimum)
schema = spec.Schema{}
schema.Type = []string{"array"}
schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
},
},
}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,max=10,min=1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, &max, schema.MaxItems)
assert.Equal(t, &min, schema.MinItems)
// wrong validate tag will be ignored.
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,max=ten,min=1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Empty(t, schema.MaxItems)
assert.Equal(t, &min, schema.MinItems)
})
t.Run("Required with oneof tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"string"}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,oneof='red book' 'green book'"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{"red book", "green book"}, schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"integer"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,oneof=1 2 3"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{1, 2, 3}, schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"array"}
schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
},
},
}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,oneof=red green yellow"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{"red", "green", "yellow"}, schema.Items.Schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,oneof='red green' blue 'c0x2Cc' 'd0x7Cd'"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{"red green", "blue", "c,c", "d|d"}, schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,oneof='c0x9Ab' book"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{"c0x9Ab", "book"}, schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" binding:"oneof=foo bar" validate:"required,oneof=foo bar" enums:"a,b,c"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{"a", "b", "c"}, schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"string"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" binding:"oneof=aa bb" validate:"required,oneof=foo bar"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, []interface{}{"aa", "bb"}, schema.Enum)
})
t.Run("Required with unique tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"array"}
schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
},
},
}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,unique"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, true, schema.UniqueItems)
})
t.Run("All tag", func(t *testing.T) {
t.Parallel()
schema := spec.Schema{}
schema.Type = []string{"array"}
schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
},
},
}
err := newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,unique,max=10,min=1,oneof=a0x2Cc 'c0x7Cd book',omitempty,dive,max=1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, true, schema.UniqueItems)
max := int64(10)
min := int64(1)
assert.Equal(t, &max, schema.MaxItems)
assert.Equal(t, &min, schema.MinItems)
assert.Equal(t, []interface{}{"a,c", "c|d book"}, schema.Items.Schema.Enum)
schema = spec.Schema{}
schema.Type = []string{"array"}
schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
},
},
}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,oneof=,max=10=90,min=1"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Empty(t, schema.UniqueItems)
assert.Empty(t, schema.MaxItems)
assert.Equal(t, &min, schema.MinItems)
schema = spec.Schema{}
schema.Type = []string{"array"}
schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: []string{"string"},
},
},
}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,max=10,min=one"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Equal(t, &max, schema.MaxItems)
assert.Empty(t, schema.MinItems)
schema = spec.Schema{}
schema.Type = []string{"integer"}
err = newTagBaseFieldParser(
&Parser{},
&ast.Field{Tag: &ast.BasicLit{
Value: `json:"test" validate:"required,oneof=one two"`,
}},
).ComplementSchema(&schema)
assert.NoError(t, err)
assert.Empty(t, schema.Enum)
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/brucelin-cn/swag.git
git@gitee.com:brucelin-cn/swag.git
brucelin-cn
swag
swag
master

搜索帮助