1 Star 0 Fork 0

lchjczw/swag

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
operation_test.go 25.01 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
package swag
import (
"encoding/json"
"go/ast"
goparser "go/parser"
"go/token"
"testing"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
)
func TestParseEmptyComment(t *testing.T) {
operation := NewOperation()
err := operation.ParseComment("//", nil)
assert.NoError(t, err)
}
func TestParseTagsComment(t *testing.T) {
expected := `{
"tags": [
"pet",
"store",
"user"
]
}`
comment := `/@Tags pet, store,user`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
assert.Equal(t, expected, string(b))
}
func TestParseAcceptComment(t *testing.T) {
expected := `{
"consumes": [
"application/json",
"text/xml",
"text/plain",
"text/html",
"multipart/form-data",
"application/x-www-form-urlencoded",
"application/vnd.api+json",
"application/x-json-stream",
"application/octet-stream",
"image/png",
"image/jpeg",
"image/gif",
"application/xhtml+xml",
"application/health+json"
]
}`
comment := `/@Accept json,xml,plain,html,mpfd,x-www-form-urlencoded,json-api,json-stream,octet-stream,png,jpeg,gif,application/xhtml+xml,application/health+json`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
assert.JSONEq(t, expected, string(b))
}
func TestParseAcceptCommentErr(t *testing.T) {
comment := `/@Accept unknown`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseProduceComment(t *testing.T) {
expected := `{
"produces": [
"application/json",
"text/xml",
"text/plain",
"text/html",
"multipart/form-data",
"application/x-www-form-urlencoded",
"application/vnd.api+json",
"application/x-json-stream",
"application/octet-stream",
"image/png",
"image/jpeg",
"image/gif",
"application/health+json"
]
}`
comment := `/@Produce json,xml,plain,html,mpfd,x-www-form-urlencoded,json-api,json-stream,octet-stream,png,jpeg,gif,application/health+json`
operation := new(Operation)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
assert.JSONEq(t, expected, string(b))
}
func TestParseProduceCommentErr(t *testing.T) {
comment := `/@Produce foo`
operation := new(Operation)
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseRouterComment(t *testing.T) {
comment := `/@Router /customer/get-wishlist/{wishlist_id} [get]`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
assert.Equal(t, "/customer/get-wishlist/{wishlist_id}", operation.Path)
assert.Equal(t, "GET", operation.HTTPMethod)
}
func TestParseRouterCommentWithPlusSign(t *testing.T) {
comment := `/@Router /customer/get-wishlist/{proxy+} [post]`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
assert.Equal(t, "/customer/get-wishlist/{proxy+}", operation.Path)
assert.Equal(t, "POST", operation.HTTPMethod)
}
func TestParseRouterCommentOccursErr(t *testing.T) {
comment := `/@Router /customer/get-wishlist/{wishlist_id}`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseResponseCommentWithObjectType(t *testing.T) {
comment := `@Success 200 {object} model.OrderRow "Error message, if code != 200`
operation := NewOperation()
operation.parser = New()
operation.parser.TypeDefinitions["model"] = make(map[string]*ast.TypeSpec)
operation.parser.TypeDefinitions["model"]["OrderRow"] = &ast.TypeSpec{}
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
response := operation.Responses.StatusCodeResponses[200]
assert.Equal(t, `Error message, if code != 200`, response.Description)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "Error message, if code != 200",
"schema": {
"$ref": "#/definitions/model.OrderRow"
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithObjectTypeInSameFile(t *testing.T) {
comment := `@Success 200 {object} testOwner "Error message, if code != 200"`
operation := NewOperation()
operation.parser = New()
operation.parser.TypeDefinitions["swag"] = make(map[string]*ast.TypeSpec)
operation.parser.TypeDefinitions["swag"]["testOwner"] = &ast.TypeSpec{}
fset := token.NewFileSet()
astFile, err := goparser.ParseFile(fset, "operation_test.go", `package swag
type testOwner struct {
}
`, goparser.ParseComments)
assert.NoError(t, err)
err = operation.ParseComment(comment, astFile)
assert.NoError(t, err)
response := operation.Responses.StatusCodeResponses[200]
assert.Equal(t, `Error message, if code != 200`, response.Description)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "Error message, if code != 200",
"schema": {
"$ref": "#/definitions/swag.testOwner"
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithObjectTypeAnonymousField(t *testing.T) {
//TODO: test Anonymous
}
func TestParseResponseCommentWithObjectTypeErr(t *testing.T) {
comment := `@Success 200 {object} model.OrderRow "Error message, if code != 200"`
operation := NewOperation()
operation.parser = New()
operation.parser.TypeDefinitions["model"] = make(map[string]*ast.TypeSpec)
operation.parser.TypeDefinitions["model"]["notexist"] = &ast.TypeSpec{}
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseResponseCommentWithArrayType(t *testing.T) {
comment := `@Success 200 {array} model.OrderRow "Error message, if code != 200`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
response := operation.Responses.StatusCodeResponses[200]
assert.Equal(t, `Error message, if code != 200`, response.Description)
assert.Equal(t, spec.StringOrArray{"array"}, response.Schema.Type)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "Error message, if code != 200",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/model.OrderRow"
}
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithBasicType(t *testing.T) {
comment := `@Success 200 {string} string "it's ok'"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "it's ok'",
"schema": {
"type": "string"
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseEmptyResponseComment(t *testing.T) {
comment := `@Success 200 "it's ok"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "it's ok"
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithHeader(t *testing.T) {
comment := `@Success 200 "it's ok"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
comment = `@Header 200 {string} Token "qwerty"`
err = operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, err := json.MarshalIndent(operation, "", " ")
assert.NoError(t, err)
expected := `{
"responses": {
"200": {
"description": "it's ok",
"headers": {
"Token": {
"type": "string",
"description": "qwerty"
}
}
}
}
}`
assert.Equal(t, expected, string(b))
comment = `@Header 200 "Mallformed"`
err = operation.ParseComment(comment, nil)
assert.Error(t, err, "ParseComment should not fail")
}
func TestParseEmptyResponseOnlyCode(t *testing.T) {
comment := `@Success 200`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentParamMissing(t *testing.T) {
operation := NewOperation()
paramLenErrComment := `@Success notIntCode {string}`
paramLenErr := operation.ParseComment(paramLenErrComment, nil)
assert.EqualError(t, paramLenErr, `can not parse response comment "notIntCode {string}"`)
}
// Test ParseParamComment
func TestParseParamCommentByPathType(t *testing.T) {
comment := `@Param some_id path int true "Some ID"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "path",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}
// Test ParseParamComment Query Params
func TestParseParamCommentBodyArray(t *testing.T) {
comment := `@Param names body []string true "Users List"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"description": "Users List",
"name": "names",
"in": "body",
"required": true,
"schema": {
"type": "string",
"items": {
"type": "string"
}
}
}
]
}`
assert.Equal(t, expected, string(b))
}
// Test ParseParamComment Query Params
func TestParseParamCommentQueryArray(t *testing.T) {
comment := `@Param names query []string true "Users List"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"description": "Users List",
"name": "names",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByID(t *testing.T) {
comment := `@Param unsafe_id[lte] query int true "Unsafe query param"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "integer",
"description": "Unsafe query param",
"name": "unsafe_id[lte]",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByQueryType(t *testing.T) {
comment := `@Param some_id query int true "Some ID"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByBodyType(t *testing.T) {
comment := `@Param some_id body model.OrderRow true "Some ID"`
operation := NewOperation()
operation.parser = New()
operation.parser.TypeDefinitions["model"] = make(map[string]*ast.TypeSpec)
operation.parser.TypeDefinitions["model"]["OrderRow"] = &ast.TypeSpec{}
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"description": "Some ID",
"name": "some_id",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/model.OrderRow"
}
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByBodyTypeErr(t *testing.T) {
comment := `@Param some_id body model.OrderRow true "Some ID"`
operation := NewOperation()
operation.parser = New()
operation.parser.TypeDefinitions["model"] = make(map[string]*ast.TypeSpec)
operation.parser.TypeDefinitions["model"]["notexist"] = &ast.TypeSpec{}
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseParamCommentByFormDataType(t *testing.T) {
comment := `@Param file formData file true "this is a test file"`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "file",
"description": "this is a test file",
"name": "file",
"in": "formData",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByFormDataTypeUint64(t *testing.T) {
comment := `@Param file formData uint64 true "this is a test file"`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "integer",
"description": "this is a test file",
"name": "file",
"in": "formData",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByNotSupportedType(t *testing.T) {
comment := `@Param some_id not_supported int true "Some ID"`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseParamCommentNotMatch(t *testing.T) {
comment := `@Param some_id body mock true`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseParamCommentByEnums(t *testing.T) {
comment := `@Param some_id query string true "Some ID" Enums(A, B, C)`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"enum": [
"A",
"B",
"C"
],
"type": "string",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
comment = `@Param some_id query int true "Some ID" Enums(1, 2, 3)`
operation = NewOperation()
err = operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ = json.MarshalIndent(operation, "", " ")
expected = `{
"parameters": [
{
"enum": [
1,
2,
3
],
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
comment = `@Param some_id query number true "Some ID" Enums(1.1, 2.2, 3.3)`
operation = NewOperation()
err = operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ = json.MarshalIndent(operation, "", " ")
expected = `{
"parameters": [
{
"enum": [
1.1,
2.2,
3.3
],
"type": "number",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
comment = `@Param some_id query bool true "Some ID" Enums(true, false)`
operation = NewOperation()
err = operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ = json.MarshalIndent(operation, "", " ")
expected = `{
"parameters": [
{
"enum": [
true,
false
],
"type": "boolean",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
operation = NewOperation()
comment = `@Param some_id query int true "Some ID" Enums(A, B, C)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query number true "Some ID" Enums(A, B, C)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query boolean true "Some ID" Enums(A, B, C)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query Document true "Some ID" Enums(A, B, C)`
assert.Error(t, operation.ParseComment(comment, nil))
}
func TestParseParamCommentByMaxLength(t *testing.T) {
comment := `@Param some_id query string true "Some ID" MaxLength(10)`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"maxLength": 10,
"type": "string",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
comment = `@Param some_id query int true "Some ID" MaxLength(10)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query string true "Some ID" MaxLength(Goopher)`
assert.Error(t, operation.ParseComment(comment, nil))
}
func TestParseParamCommentByMinLength(t *testing.T) {
comment := `@Param some_id query string true "Some ID" MinLength(10)`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"minLength": 10,
"type": "string",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
comment = `@Param some_id query int true "Some ID" MinLength(10)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query string true "Some ID" MinLength(Goopher)`
assert.Error(t, operation.ParseComment(comment, nil))
}
func TestParseParamCommentByMininum(t *testing.T) {
comment := `@Param some_id query int true "Some ID" Mininum(10)`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"minimum": 10,
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
comment = `@Param some_id query string true "Some ID" Mininum(10)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query integer true "Some ID" Mininum(Goopher)`
assert.Error(t, operation.ParseComment(comment, nil))
}
func TestParseParamCommentByMaxinum(t *testing.T) {
comment := `@Param some_id query int true "Some ID" Maxinum(10)`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"maximum": 10,
"type": "integer",
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
comment = `@Param some_id query string true "Some ID" Maxinum(10)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query integer true "Some ID" Maxinum(Goopher)`
assert.Error(t, operation.ParseComment(comment, nil))
}
func TestParseParamCommentByDefault(t *testing.T) {
comment := `@Param some_id query int true "Some ID" Default(10)`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "integer",
"default": 10,
"description": "Some ID",
"name": "some_id",
"in": "query",
"required": true
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseIdComment(t *testing.T) {
comment := `@Id myOperationId`
operation := NewOperation()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
assert.Equal(t, "myOperationId", operation.ID)
}
func TestFindTypeDefCoreLib(t *testing.T) {
spec, err := findTypeDef("net/http", "Request")
assert.NoError(t, err)
assert.NotNil(t, spec)
}
func TestFindTypeDefExternalPkg(t *testing.T) {
spec, err := findTypeDef("github.com/stretchr/testify/assert", "Assertions")
assert.NoError(t, err)
assert.NotNil(t, spec)
}
func TestFindTypeDefInvalidPkg(t *testing.T) {
spec, err := findTypeDef("does-not-exist", "foo")
assert.Error(t, err)
assert.Nil(t, spec)
}
func TestParseSecurityComment(t *testing.T) {
comment := `@Security OAuth2Implicit[read, write]`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"security": [
{
"OAuth2Implicit": [
"read",
"write"
]
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseMultiDescription(t *testing.T) {
comment := `@Description line one`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
comment = `@Tags multi`
err = operation.ParseComment(comment, nil)
assert.NoError(t, err)
comment = `@Description line two x`
err = operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `"description": "line one\nline two x"`
assert.Contains(t, string(b), expected)
}
func TestParseSummary(t *testing.T) {
comment := `@summary line one`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
comment = `@Summary line one`
err = operation.ParseComment(comment, nil)
assert.NoError(t, err)
}
func TestParseDeprecationDescription(t *testing.T) {
comment := `@Deprecated`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
if !operation.Deprecated {
t.Error("Failed to parse @deprecated comment")
}
}
func TestRegisterSchemaType(t *testing.T) {
operation := NewOperation()
assert.NoError(t, operation.registerSchemaType("string", nil))
fset := token.NewFileSet()
astFile, err := goparser.ParseFile(fset, "main.go", `package main
import "timer"
`, goparser.ParseComments)
assert.NoError(t, err)
operation.parser = New()
assert.Error(t, operation.registerSchemaType("timer.Location", astFile))
}
func TestParseExtentions(t *testing.T) {
// Fail if there are no args for attributes.
{
comment := `@x-amazon-apigateway-integration`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.EqualError(t, err, "annotation @x-amazon-apigateway-integration need a value")
}
// Fail if args of attributes are broken.
{
comment := `@x-amazon-apigateway-integration ["broken"}]`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.EqualError(t, err, "annotation @x-amazon-apigateway-integration need a valid json value")
}
// OK
{
comment := `@x-amazon-apigateway-integration {"uri": "${some_arn}", "passthroughBehavior": "when_no_match", "httpMethod": "POST", "type": "aws_proxy"}`
operation := NewOperation()
operation.parser = New()
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
expected := `{
"x-amazon-apigateway-integration": {
"httpMethod": "POST",
"passthroughBehavior": "when_no_match",
"type": "aws_proxy",
"uri": "${some_arn}"
}
}`
b, _ := json.MarshalIndent(operation, "", " ")
assert.Equal(t, expected, string(b))
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/lchjczw/swag.git
git@gitee.com:lchjczw/swag.git
lchjczw
swag
swag
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385