Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the regular expression to fix the issue #853 caused by PR #837 #854

Merged
merged 7 commits into from
Dec 9, 2020
16 changes: 8 additions & 8 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,9 +603,9 @@ func findTypeDef(importPath, typeName string) (*ast.TypeSpec, error) {
return nil, fmt.Errorf("type spec not found")
}

var responsePattern = regexp.MustCompile(`([\w,]+)[\s]+([\w\{\}]+)[\s]+([\w\-\.\/\{\}=,\[\]]+)[^"]*(.*)?`)
var responsePattern = regexp.MustCompile(`^([\w,]+)[\s]+([\w\{\}]+)[\s]+([\w\-\.\/\{\}=,\[\]]+)[^"]*(.*)?`)

//RepsonseType{data1=Type1,data2=Type2}
//ResponseType{data1=Type1,data2=Type2}
var combinedPattern = regexp.MustCompile(`^([\w\-\.\/\[\]]+)\{(.*)\}$`)

func (operation *Operation) parseObjectSchema(refType string, astFile *ast.File) (*spec.Schema, error) {
Expand Down Expand Up @@ -748,13 +748,13 @@ func (operation *Operation) ParseResponseComment(commentLine string, astFile *as
operation.DefaultResponse().Schema = schema
operation.DefaultResponse().Description = responseDescription
} else if code, err := strconv.Atoi(codeStr); err == nil {
if responseDescription == "" {
responseDescription = http.StatusText(code)
}

operation.AddResponse(code, &spec.Response{
resp := &spec.Response{
ResponseProps: spec.ResponseProps{Schema: schema, Description: responseDescription},
})
}
if resp.Description == "" {
resp.Description = http.StatusText(code)
}
operation.AddResponse(code, resp)
} else {
return fmt.Errorf("can not parse response comment \"%s\"", commentLine)
}
Expand Down
20 changes: 10 additions & 10 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -672,7 +672,7 @@ func TestParseResponseCommentWithBasicType(t *testing.T) {
}

func TestParseResponseCommentWithBasicTypeAndCodes(t *testing.T) {
comment := `@Success 200,201,default {string} string "it's ok'"`
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")
Expand All @@ -681,19 +681,19 @@ func TestParseResponseCommentWithBasicTypeAndCodes(t *testing.T) {
expected := `{
"responses": {
"200": {
"description": "it's ok'",
"description": "it's ok",
"schema": {
"type": "string"
}
},
"201": {
"description": "it's ok'",
"description": "it's ok",
"schema": {
"type": "string"
}
},
"default": {
"description": "it's ok'",
"description": "it's ok",
"schema": {
"type": "string"
}
Expand All @@ -704,7 +704,7 @@ func TestParseResponseCommentWithBasicTypeAndCodes(t *testing.T) {
}

func TestParseEmptyResponseComment(t *testing.T) {
comment := `@Success 200 "it's ok"`
comment := `@Success 200 "it is ok"`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
Expand All @@ -714,15 +714,15 @@ func TestParseEmptyResponseComment(t *testing.T) {
expected := `{
"responses": {
"200": {
"description": "it's ok"
"description": "it is ok"
}
}
}`
assert.Equal(t, expected, string(b))
}

func TestParseEmptyResponseCommentWithCodes(t *testing.T) {
comment := `@Success 200,201,default "it's ok"`
comment := `@Success 200,201,default "it is ok"`
operation := NewOperation(nil)
err := operation.ParseComment(comment, nil)
assert.NoError(t, err, "ParseComment should not fail")
Expand All @@ -732,13 +732,13 @@ func TestParseEmptyResponseCommentWithCodes(t *testing.T) {
expected := `{
"responses": {
"200": {
"description": "it's ok"
"description": "it is ok"
},
"201": {
"description": "it's ok"
"description": "it is ok"
},
"default": {
"description": "it's ok"
"description": "it is ok"
}
}
}`
Expand Down