Skip to content

Commit

Permalink
fix: parsing panics on default response (#1068)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubogdan authored Dec 1, 2021
1 parent a7065ad commit 47d0cd2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
33 changes: 16 additions & 17 deletions operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -831,29 +831,28 @@ func (operation *Operation) ParseResponseComment(commentLine string, astFile *as
return err
}

responseDescription := strings.Trim(matches[4], "\"")
description := strings.Trim(matches[4], "\"")
schema, err := operation.parseAPIObjectSchema(strings.Trim(matches[2], "{}"), matches[3], astFile)
if err != nil {
return err
}

for _, codeStr := range strings.Split(matches[1], ",") {
if strings.EqualFold(codeStr, defaultTag) {
operation.DefaultResponse().Schema = schema
operation.DefaultResponse().Description = responseDescription
operation.DefaultResponse().WithSchema(schema).WithDescription(description)

continue
}
code, err := strconv.Atoi(codeStr)
if err != nil {
return fmt.Errorf("can not parse response comment \"%s\"", commentLine)
}
resp := &spec.Response{
ResponseProps: spec.ResponseProps{Schema: schema, Description: responseDescription},
}
if resp.Description == "" {
resp.Description = http.StatusText(code)

resp := spec.NewResponse().WithSchema(schema).WithDescription(description)
if description == "" {
resp.WithDescription(http.StatusText(code))
}

operation.AddResponse(code, resp)
}

Expand Down Expand Up @@ -945,10 +944,10 @@ func (operation *Operation) ParseEmptyResponseComment(commentLine string) error
return fmt.Errorf("can not parse response comment \"%s\"", commentLine)
}

responseDescription := strings.Trim(matches[2], "\"")
description := strings.Trim(matches[2], "\"")
for _, codeStr := range strings.Split(matches[1], ",") {
if strings.EqualFold(codeStr, defaultTag) {
operation.DefaultResponse().Description = responseDescription
operation.DefaultResponse().WithDescription(description)

continue
}
Expand All @@ -958,9 +957,7 @@ func (operation *Operation) ParseEmptyResponseComment(commentLine string) error
return fmt.Errorf("can not parse response comment \"%s\"", commentLine)
}

var response spec.Response
response.Description = responseDescription
operation.AddResponse(code, &response)
operation.AddResponse(code, spec.NewResponse().WithDescription(description))
}

return nil
Expand All @@ -979,18 +976,20 @@ func (operation *Operation) ParseEmptyResponseOnly(commentLine string) error {
return fmt.Errorf("can not parse response comment \"%s\"", commentLine)
}

var response spec.Response
// response.Description = http.StatusText(code)
operation.AddResponse(code, &response)
operation.AddResponse(code, spec.NewResponse())
}

return nil
}

// DefaultResponse return the default response member pointer.
func (operation *Operation) DefaultResponse() *spec.Response {
if operation.Responses == nil {
operation.Responses = &spec.Responses{}
}

if operation.Responses.Default == nil {
operation.Responses.Default = &spec.Response{}
operation.Responses.Default = spec.NewResponse()
}

return operation.Responses.Default
Expand Down
21 changes: 21 additions & 0 deletions operation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,27 @@ func TestParseRouterCommentMethodMissingErr(t *testing.T) {
assert.Error(t, err)
}

func TestOperation_ParseResponseWithDefault(t *testing.T) {
t.Parallel()

comment := `@Success default {object} nil "An empty response"`
operation := NewOperation(nil)

err := operation.ParseComment(comment, nil)
assert.NoError(t, err)

assert.Equal(t, "An empty response", operation.Responses.Default.Description)

comment = `@Success 200,default {string} Response "A response"`
operation = NewOperation(nil)

err = operation.ParseComment(comment, nil)
assert.NoError(t, err)

assert.Equal(t, "A response", operation.Responses.Default.Description)
assert.Equal(t, "A response", operation.Responses.StatusCodeResponses[200].Description)
}

func TestParseResponseSuccessCommentWithEmptyResponse(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit 47d0cd2

Please sign in to comment.