fix: allow response error handler to set status code#1963
Conversation
change the order the visit response body function sets the status code and content type so if an error occurs during json marshalling the response error handler can set the status code
|
Thanks for reporting + raising a suggested fix! Will try to look at it soon 🤞🏼 |
Resolved conflict in strict-interface.tmpl: kept PR's reordering (body encoding before headers/WriteHeader) while incorporating upstream's toGoString helper and $hasBodyVar whitespace change. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thanks for the PR and for identifying the issue — you're right that it's a problem that I used AI to analyze every possible path through this code, and this is what it found: Non-JSON content types lose headers and status codeThe
This would be a breaking regression for all non-JSON strict server responses. JSON path: headers set after writing the body are ineffectiveFor the JSON path, The fix doesn't fully achieve its stated goalSince Suggested approach: encode to a buffer firstTo properly fix this, the body should be encoded to a buffer before anything is written to the var buf bytes.Buffer
if err := json.NewEncoder(&buf).Encode(response); err != nil {
return err // nothing written to w yet, so error handler can safely set status
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
_, err := buf.WriteTo(w)
return errThis ensures:
The downside: |
JSON responses in strict server Visit*Response functions are now encoded to a bytes.Buffer before setting headers and calling WriteHeader. This ensures that if JSON encoding fails, nothing has been written to the ResponseWriter, allowing ResponseErrorHandlerFunc to set an appropriate status code (e.g. 500 instead of 200). Non-JSON content types (text, formdata, multipart, binary/io.Reader) retain the original headers-first ordering since their write patterns do not benefit from buffering. Adds regression tests for all content type paths and an integration test verifying that ResponseErrorHandlerFunc can set the status code when JSON encoding fails. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Greptile SummaryThis PR fixes a critical bug where generated Key changes:
Known limitations: Text, Multipart, and binary ( Confidence Score: 4/5
Last reviewed commit: 2b56060 |
Also buffer formdata responses before writing headers Apply the same marshal-before-headers pattern to Formdata responses: runtime.MarshalForm is called before setting Content-Type or calling WriteHeader, so if marshalling fails the error handler can still set an appropriate status code. Adds a regression test for the formdata encoding error path. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
As mentioned in #1964
Version: 2.4.1
Issue: Status code is set to 200 before marshalling data into body inside visit functions, so when an error occurs marshalling the ResponseErrorHandlerFunc cannot set the status code.
Reproduce:
Generated visit function
Proposed changes would have the output