From 8fa8055c5ff4bd34a37b4a4c5a8dab1e57e51e94 Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Thu, 21 Apr 2022 23:24:04 -0700 Subject: [PATCH 1/2] fix: accept status code 0 as default response --- context.go | 4 ++-- operation.go | 3 +++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/context.go b/context.go index 203ded7d..224c618d 100644 --- a/context.go +++ b/context.go @@ -115,7 +115,7 @@ func (c *hcontext) WriteHeader(status int) { if c.op != nil { allowed := []string{} for _, r := range c.op.responses { - if r.status == status { + if r.status == status || r.status == 0 { for _, h := range r.headers { allowed = append(allowed, h) } @@ -273,7 +273,7 @@ func (c *hcontext) writeModel(ct string, status int, model interface{}) { statuses := []string{} for _, r := range c.op.responses { statuses = append(statuses, fmt.Sprintf("%d", r.status)) - if r.status == status { + if r.status == status || r.status == 0 { responses = append(responses, r) if r.model != nil { names = append(names, r.model.Name()) diff --git a/operation.go b/operation.go index eaeff651..f41e2d03 100644 --- a/operation.go +++ b/operation.go @@ -111,6 +111,9 @@ func (o *Operation) toOpenAPI(components *oaComponents) *gabs.Container { // responses for i, resp := range o.responses { status := fmt.Sprintf("%v", resp.status) + if resp.status == 0 { + status = "default" + } doc.Set(resp.description, "responses", status, "description") headers := resp.headers From f7e947707a1bd78b4603418fedcc41b6d54988c8 Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Thu, 21 Apr 2022 23:29:44 -0700 Subject: [PATCH 2/2] test: add default repsonse test --- router_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/router_test.go b/router_test.go index d69fbbf4..626210b7 100644 --- a/router_test.go +++ b/router_test.go @@ -462,3 +462,19 @@ func TestSubResource(t *testing.T) { // Do nothing }) } + +func TestDefaultResponse(t *testing.T) { + app := newTestRouter() + + // This should not crash. + app.Resource("/").Get("get-root", "docs", NewResponse(0, "Default repsonse")).Run(func(ctx Context) { + ctx.WriteHeader(http.StatusOK) + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/", nil) + app.ServeHTTP(w, req) + + // This should not panic and should return the 200 OK + assert.Equal(t, http.StatusOK, w.Result().StatusCode) +}