1 Star 0 Fork 0

Codey/swag

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
operation_test.go 48.93 KB
一键复制 编辑 原始数据 按行查看 历史
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700
package swag
import (
"encoding/json"
goparser "go/parser"
"go/token"
"testing"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
)
func TestParseEmptyComment(t *testing.T) {
operation := NewOperation(nil)
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(nil)
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(nil)
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(nil)
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(nil)
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 TestParseRouterOnlySlash(t *testing.T) {
comment := `// @Router / [get]`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
assert.Equal(t, "/", operation.Path)
assert.Equal(t, "GET", operation.HTTPMethod)
}
func TestParseRouterCommentWithPlusSign(t *testing.T) {
comment := `/@Router /customer/get-wishlist/{proxy+} [post]`
operation := NewOperation(nil)
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 TestParseRouterCommentWithColonSign(t *testing.T) {
comment := `/@Router /customer/get-wishlist/{wishlist_id}:move [post]`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
assert.Equal(t, "/customer/get-wishlist/{wishlist_id}:move", operation.Path)
assert.Equal(t, "POST", operation.HTTPMethod)
}
func TestParseRouterCommentNoColonSignAtPathStartErr(t *testing.T) {
comment := `/@Router :customer/get-wishlist/{wishlist_id}:move [post]`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseRouterCommentMethodSeparationErr(t *testing.T) {
comment := `/@Router /api/{id}|,*[get`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseRouterCommentMethodMissingErr(t *testing.T) {
comment := `/@Router /customer/get-wishlist/{wishlist_id}`
operation := NewOperation(nil)
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(nil)
operation.parser.addTestType("model.OrderRow")
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 TestParseResponseCommentWithNestedPrimitiveType(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data=string,data2=int} "Error message, if code != 200`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
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": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
},
"data2": {
"type": "integer"
}
}
}
]
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithNestedPrimitiveArrayType(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data=[]string,data2=[]int} "Error message, if code != 200`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
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": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"type": "string"
}
},
"data2": {
"type": "array",
"items": {
"type": "integer"
}
}
}
}
]
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithNestedObjectType(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data=model.Payload,data2=model.Payload2} "Error message, if code != 200`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
operation.parser.addTestType("model.Payload")
operation.parser.addTestType("model.Payload2")
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": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/model.Payload"
},
"data2": {
"$ref": "#/definitions/model.Payload2"
}
}
}
]
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithNestedArrayObjectType(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data=[]model.Payload,data2=[]model.Payload2} "Error message, if code != 200`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
operation.parser.addTestType("model.Payload")
operation.parser.addTestType("model.Payload2")
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": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Payload"
}
},
"data2": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Payload2"
}
}
}
}
]
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithNestedFields(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data1=int,data2=[]int,data3=model.Payload,data4=[]model.Payload} "Error message, if code != 200`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
operation.parser.addTestType("model.Payload")
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": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data1": {
"type": "integer"
},
"data2": {
"type": "array",
"items": {
"type": "integer"
}
},
"data3": {
"$ref": "#/definitions/model.Payload"
},
"data4": {
"type": "array",
"items": {
"$ref": "#/definitions/model.Payload"
}
}
}
}
]
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithDeepNestedFields(t *testing.T) {
comment := `@Success 200 {object} model.CommonHeader{data1=int,data2=[]int,data3=model.Payload{data1=int,data2=model.DeepPayload},data4=[]model.Payload{data1=[]int,data2=[]model.DeepPayload}} "Error message, if code != 200`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
operation.parser.addTestType("model.Payload")
operation.parser.addTestType("model.DeepPayload")
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": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data1": {
"type": "integer"
},
"data2": {
"type": "array",
"items": {
"type": "integer"
}
},
"data3": {
"allOf": [
{
"$ref": "#/definitions/model.Payload"
},
{
"type": "object",
"properties": {
"data1": {
"type": "integer"
},
"data2": {
"$ref": "#/definitions/model.DeepPayload"
}
}
}
]
},
"data4": {
"type": "array",
"items": {
"allOf": [
{
"$ref": "#/definitions/model.Payload"
},
{
"type": "object",
"properties": {
"data1": {
"type": "array",
"items": {
"type": "integer"
}
},
"data2": {
"type": "array",
"items": {
"$ref": "#/definitions/model.DeepPayload"
}
}
}
}
]
}
}
}
}
]
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithNestedArrayMapFields(t *testing.T) {
comment := `@Success 200 {object} []map[string]model.CommonHeader{data1=[]map[string]model.Payload,data2=map[string][]int} "Error message, if code != 200`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
operation.parser.addTestType("model.Payload")
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": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data1": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/model.Payload"
}
}
},
"data2": {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "integer"
}
}
}
}
}
]
}
}
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithObjectTypeInSameFile(t *testing.T) {
comment := `@Success 200 {object} testOwner "Error message, if code != 200"`
operation := NewOperation(nil)
operation.parser.addTestType("swag.testOwner")
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(nil)
operation.parser.addTestType("model.notexist")
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(nil)
operation.parser.addTestType("model.OrderRow")
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(nil)
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 TestParseResponseCommentWithBasicTypeAndCodes(t *testing.T) {
comment := `@Success 200,201,default {string} string "it's ok"`
operation := NewOperation(nil)
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"
}
},
"201": {
"description": "it's ok",
"schema": {
"type": "string"
}
},
"default": {
"description": "it's ok",
"schema": {
"type": "string"
}
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseEmptyResponseComment(t *testing.T) {
comment := `@Success 200 "it is ok"`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "it is ok"
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseEmptyResponseCommentWithCodes(t *testing.T) {
comment := `@Success 200,201,default "it is ok"`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": "it is ok"
},
"201": {
"description": "it is ok"
},
"default": {
"description": "it is ok"
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentWithHeader(t *testing.T) {
comment := `@Success 200 "it's ok"`
operation := NewOperation(nil)
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 TestParseResponseCommentWithHeaderForCodes(t *testing.T) {
operation := NewOperation(nil)
comment := `@Success 200,201,default "it's ok"`
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
comment = `@Header 200,201,default {string} Token "qwerty"`
err = operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
comment = `@Header all {string} Token2 "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"
},
"Token2": {
"type": "string",
"description": "qwerty"
}
}
},
"201": {
"description": "it's ok",
"headers": {
"Token": {
"type": "string",
"description": "qwerty"
},
"Token2": {
"type": "string",
"description": "qwerty"
}
}
},
"default": {
"description": "it's ok",
"headers": {
"Token": {
"type": "string",
"description": "qwerty"
},
"Token2": {
"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(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": ""
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseEmptyResponseOnlyCodes(t *testing.T) {
comment := `@Success 200,201,default`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"responses": {
"200": {
"description": ""
},
"201": {
"description": ""
},
"default": {
"description": ""
}
}
}`
assert.Equal(t, expected, string(b))
}
func TestParseResponseCommentParamMissing(t *testing.T) {
operation := NewOperation(nil)
paramLenErrComment := `@Success notIntCode`
paramLenErr := operation.ParseComment(paramLenErrComment, nil)
assert.EqualError(t, paramLenErr, `can not parse response comment "notIntCode"`)
paramLenErrComment = `@Success notIntCode {string} string "it ok"`
paramLenErr = operation.ParseComment(paramLenErrComment, nil)
assert.EqualError(t, paramLenErr, `can not parse response comment "notIntCode {string} string "it ok""`)
paramLenErrComment = `@Success notIntCode "it ok"`
paramLenErr = operation.ParseComment(paramLenErrComment, nil)
assert.EqualError(t, paramLenErr, `can not parse response comment "notIntCode "it ok""`)
}
// Test ParseParamComment
func TestParseParamCommentByPathType(t *testing.T) {
comment := `@Param some_id path int true "Some ID"`
operation := NewOperation(nil)
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(nil)
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": "array",
"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(nil)
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))
}
// Test ParseParamComment Query Params
func TestParseParamCommentQueryArrayFormat(t *testing.T) {
comment := `@Param names query []string true "Users List" collectionFormat(multi)`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"parameters": [
{
"type": "array",
"items": {
"type": "string"
},
"collectionFormat": "multi",
"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(nil)
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(nil)
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(nil)
operation.parser.addTestType("model.OrderRow")
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 TestParseParamCommentByBodyTypeWithDeepNestedFields(t *testing.T) {
comment := `@Param body body model.CommonHeader{data=string,data2=int} true "test deep"`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
assert.Len(t, operation.Parameters, 1)
assert.Equal(t, "test deep", operation.Parameters[0].Description)
assert.True(t, operation.Parameters[0].Required)
b, err := json.MarshalIndent(operation, "", " ")
assert.NoError(t, err)
expected := `{
"parameters": [
{
"description": "test deep",
"name": "body",
"in": "body",
"required": true,
"schema": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
},
"data2": {
"type": "integer"
}
}
}
]
}
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByBodyTypeArrayOfPrimitiveGo(t *testing.T) {
comment := `@Param some_id body []int true "Some ID"`
operation := NewOperation(nil)
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": {
"type": "array",
"items": {
"type": "integer"
}
}
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByBodyTypeArrayOfPrimitiveGoWithDeepNestedFields(t *testing.T) {
comment := `@Param body body []model.CommonHeader{data=string,data2=int} true "test deep"`
operation := NewOperation(nil)
operation.parser.addTestType("model.CommonHeader")
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
assert.Len(t, operation.Parameters, 1)
assert.Equal(t, "test deep", operation.Parameters[0].Description)
assert.True(t, operation.Parameters[0].Required)
b, err := json.MarshalIndent(operation, "", " ")
assert.NoError(t, err)
expected := `{
"parameters": [
{
"description": "test deep",
"name": "body",
"in": "body",
"required": true,
"schema": {
"type": "array",
"items": {
"allOf": [
{
"$ref": "#/definitions/model.CommonHeader"
},
{
"type": "object",
"properties": {
"data": {
"type": "string"
},
"data2": {
"type": "integer"
}
}
}
]
}
}
}
]
}`
assert.Equal(t, expected, string(b))
}
func TestParseParamCommentByBodyTypeErr(t *testing.T) {
comment := `@Param some_id body model.OrderRow true "Some ID"`
operation := NewOperation(nil)
operation.parser.addTestType("model.notexist")
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(nil)
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(nil)
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(nil)
err := operation.ParseComment(comment, nil)
assert.Error(t, err)
}
func TestParseParamCommentNotMatch(t *testing.T) {
comment := `@Param some_id body mock true`
operation := NewOperation(nil)
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(nil)
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(nil)
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(nil)
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(nil)
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(nil)
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(nil)
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(nil)
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 TestParseParamCommentByMinimum(t *testing.T) {
comment := `@Param some_id query int true "Some ID" Minimum(10)`
operation := NewOperation(nil)
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 int true "Some ID" Mininum(10)`
assert.NoError(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query string true "Some ID" Minimum(10)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query integer true "Some ID" Minimum(Goopher)`
assert.Error(t, operation.ParseComment(comment, nil))
}
func TestParseParamCommentByMaximum(t *testing.T) {
comment := `@Param some_id query int true "Some ID" Maximum(10)`
operation := NewOperation(nil)
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 int true "Some ID" Maxinum(10)`
assert.NoError(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query string true "Some ID" Maximum(10)`
assert.Error(t, operation.ParseComment(comment, nil))
comment = `@Param some_id query integer true "Some ID" Maximum(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(nil)
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(nil)
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/KyleBanks/depth", "Tree")
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(nil)
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(nil)
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(nil)
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(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
if !operation.Deprecated {
t.Error("Failed to parse @deprecated comment")
}
}
func TestParseExtentions(t *testing.T) {
// Fail if there are no args for attributes.
{
comment := `@x-amazon-apigateway-integration`
operation := NewOperation(nil)
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(nil)
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(nil)
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))
}
// Test x-tagGroups
{
comment := `@x-tagGroups [{"name":"Natural Persons","tags":["Person","PersonRisk","PersonDocuments"]}]`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err)
expected := `{
"x-tagGroups": [
{
"name": "Natural Persons",
"tags": [
"Person",
"PersonRisk",
"PersonDocuments"
]
}
]
}`
b, _ := json.MarshalIndent(operation, "", " ")
assert.Equal(t, expected, string(b))
}
}
func TestParseCodeSamples(t *testing.T) {
t.Run("Find sample by file", func(t *testing.T) {
comment := `@x-codeSamples file`
operation := NewOperation(nil, SetCodeExampleFilesDirectory("testdata/code_examples"))
operation.Summary = "example"
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "no error should be thrown")
b, _ := json.MarshalIndent(operation, "", " ")
expected := `{
"summary": "example",
"x-codeSamples": {
"lang": "JavaScript",
"source": "console.log('Hello World');"
}
}`
assert.Equal(t, expected, string(b))
})
t.Run("Example file not found", func(t *testing.T) {
comment := `@x-codeSamples file`
operation := NewOperation(nil, SetCodeExampleFilesDirectory("testdata/code_examples"))
operation.Summary = "exampel"
err := operation.ParseComment(comment, nil)
assert.Error(t, err, "error was expected, as file does not exist")
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/codey-repositories/swag.git
[email protected]:codey-repositories/swag.git
codey-repositories
swag
swag
master

搜索帮助