Repository navigation
Add warning when writing response body multiple times (fixes #4477) - #4483
raju-mechatronics wants to merge 8 commits into
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4483 +/- ##
=======================================
Coverage 98.65% 98.65%
=======================================
Files 48 48
Lines 3188 3192 +4
=======================================
+ Hits 3145 3149 +4
Misses 43 43
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
@appleboy can you please take a look at this pr |
|
Hey nice addition but when the warning fires the code still continues to render, which means the response could still gets corrupted, right? Shouldn't we |
I was waiting for reviewer response on this but it's for backward compatibility. Watch #4477 for details. It's seems multiple response is allowed in gin. |
There was a problem hiding this comment.
Pull request overview
This PR adds a debug-mode warning when a handler attempts to write a response body more than once during the same request, addressing silent concatenation (e.g., multiple JSON/XML writes) that can produce invalid output.
Changes:
- Add a debug warning in
Context.Render()when a second render occurs after the response has already been written. - Exempt
code == -1(used for SSE/stream-like cases) andsse.Eventfrom the warning. - Add tests to validate the warning for multiple JSON writes and ensure no warning for repeated SSE writes.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 6 comments.
| File | Description |
|---|---|
| context.go | Emits a debug warning when rendering after the response is already written, with special-casing for SSE/-1 code. |
| context_test.go | Adds tests covering the new warning behavior for JSON and SSE. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
move to the next milestone |
Add warning for multiple response writes
Fixes #4477
Description
Adds a warning when the response body is written multiple times in the same request. This prevents silent concatenation of multiple JSON/XML responses, which often produces invalid output.
Changes
Context.Render()when response body already writtenExample
Output:
Checklist
masterbranch.docs/doc.md.