Skip to content

Commit

Permalink
fix invalid response type: array of strings (swaggo#173)
Browse files Browse the repository at this point in the history
* fix invalid response type: array of strings

* fix lint
  • Loading branch information
pei0804 authored and easonlin404 committed Jul 25, 2018
1 parent 4b32e23 commit 741541c
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 8 deletions.
22 changes: 16 additions & 6 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,14 +443,24 @@ func (operation *Operation) ParseResponseComment(commentLine string) error {
}

if schemaType == "array" {
response.Schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.Ref{Ref: jsonreference.MustCreateRef("#/definitions/" + refType)},
refType = TransToValidSchemeType(refType)
if IsPrimitiveType(refType) {
response.Schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Type: spec.StringOrArray{refType},
},
},
},
}
} else {
response.Schema.Items = &spec.SchemaOrArray{
Schema: &spec.Schema{
SchemaProps: spec.SchemaProps{
Ref: spec.Ref{Ref: jsonreference.MustCreateRef("#/definitions/" + refType)},
},
},
}
}

}

if operation.Responses == nil {
Expand Down
9 changes: 9 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ func TestParseSimpleApi(t *testing.T) {
"$ref": "#/definitions/web.APIError"
}
},
"401": {
"description": "Unauthorized",
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
},
"404": {
"description": "Can not find ID",
"schema": {
Expand Down
11 changes: 9 additions & 2 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@ import "fmt"

// CheckSchemaType TODO: NEEDS COMMENT INFO
func CheckSchemaType(typeName string) {
if !IsPrimitiveType(typeName) {
panic(fmt.Errorf("%s is not basic types", typeName))
}
}

// IsPrimitiveType determine whether the type name is a primitive type
func IsPrimitiveType(typeName string) bool {
switch typeName {
case "string", "number", "integer", "boolean", "array", "object":

return true
default:
panic(fmt.Errorf("%s is not basic types", typeName))
return false
}
}

Expand Down
1 change: 1 addition & 0 deletions testdata/simple/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func GetStructArrayByString(c *gin.Context) {
// @Param file formData file true "this is a test file"
// @Success 200 {string} string "ok"
// @Failure 400 {object} web.APIError "We need ID!!"
// @Failure 401 {array} string
// @Failure 404 {object} web.APIError "Can not find ID"
// @Router /file/upload [post]
func Upload(ctx *gin.Context) {
Expand Down

0 comments on commit 741541c

Please sign in to comment.