Skip to content

feat: add warning for multiple response writes#4603

Open
xingzihai wants to merge 1 commit intogin-gonic:masterfrom
xingzihai:feat/issue-4477-multiple-writes-warning
Open

feat: add warning for multiple response writes#4603
xingzihai wants to merge 1 commit intogin-gonic:masterfrom
xingzihai:feat/issue-4477-multiple-writes-warning

Conversation

@xingzihai
Copy link
Copy Markdown

Summary

This PR adds a warning when multiple response writes are attempted on the same context, addressing issue #4477.

Problem

When a handler writes a response multiple times (e.g., calling c.JSON() twice, or mixing c.String() with c.JSON()), the second write silently fails or causes unexpected behavior. Users often encounter this issue without understanding what went wrong.

Solution

Added a warning mechanism that logs a clear warning message when multiple response writes are detected.

Changes

  • Added detection logic in context.go
  • Warning message helps developers identify and fix the issue

Testing

  • Manual testing shows warnings appear correctly
  • Existing tests pass

Fixes #4477

Add debug warning when multiple response writes are detected in Context.Render().
This helps developers identify potential issues where multiple calls to response
writing methods (JSON, XML, String, etc.) would concatenate outputs.

Changes:
- Add wroteResponse bool field to Context struct
- Reset wroteResponse in Context.reset() method
- Check and warn in Context.Render() before writing
- Set wroteResponse=true after successful write

The warning is only printed in debug mode using debugPrint(), which is
controlled by gin.SetMode(). In release mode, no warning is printed.
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.44%. Comparing base (3dc1cd6) to head (094ea82).
⚠️ Report is 275 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4603      +/-   ##
==========================================
- Coverage   99.21%   98.44%   -0.78%     
==========================================
  Files          42       48       +6     
  Lines        3182     3143      -39     
==========================================
- Hits         3157     3094      -63     
- Misses         17       40      +23     
- Partials        8        9       +1     
Flag Coverage Δ
?
--ldflags="-checklinkname=0" -tags sonic 98.42% <100.00%> (?)
-tags go_json 98.30% <100.00%> (?)
-tags nomsgpack 98.35% <100.00%> (?)
go-1.18 ?
go-1.19 ?
go-1.20 ?
go-1.21 ?
go-1.25 98.37% <100.00%> (?)
go-1.26 98.44% <100.00%> (?)
macos-latest 98.37% <100.00%> (-0.84%) ⬇️
ubuntu-latest 98.44% <100.00%> (-0.78%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add warning for multiple response writes

1 participant