Skip to content

Commit fbffb62

Browse files
committed
Context: json should not send status code before serialization is complete
1 parent 8e41156 commit fbffb62

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

context.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,9 +467,13 @@ func (c *Context) json(code int, i any, indent string) error {
467467
// as JSONSerializer.Serialize can fail, and in that case we need to delay sending status code to the client until
468468
// (global) error handler decides correct status code for the error to be sent to the client.
469469
// For that we need to use writer that can store the proposed status code until the first Write is called.
470-
resp := c.Response()
471-
c.SetResponse(&delayedStatusWriter{ResponseWriter: resp, status: code})
472-
defer c.SetResponse(resp)
470+
if r, err := UnwrapResponse(c.response); err == nil {
471+
r.Status = code
472+
} else {
473+
resp := c.Response()
474+
c.SetResponse(&delayedStatusWriter{ResponseWriter: resp, status: code})
475+
defer c.SetResponse(resp)
476+
}
473477

474478
return c.echo.JSONSerializer.Serialize(c, i, indent)
475479
}

0 commit comments

Comments
 (0)