Repository navigation
fix(response): set status and Content-Length for JSON null body - #2022
NgoQuocViet2001 wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review.
|
Reviewer's GuideFix JSON responses whose body is null or undefined by treating the serialized null body like the normal response path: defaulting to 200, preserving explicit statuses, and setting Content-Length to 4; accompanying tests cover status and stale-length regressions. Sequence diagram for JSON null response handlingsequenceDiagram
participant App
participant Context
participant Response
App->>Context: set type to application/json
App->>Context: set body to null
Context->>Response: set _body to null
alt status not explicitly set
Context->>Response: set status to 200
else status explicitly set
Context->>Response: preserve explicit status
end
Context->>Response: set length to Buffer.byteLength(_body)
Response-->>App: send JSON null with Content-Length 4
File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2022 +/- ##
=======================================
Coverage 99.90% 99.90%
=======================================
Files 9 9
Lines 2144 2146 +2
=======================================
+ Hits 2142 2144 +2
Misses 2 2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
agammann
left a comment
There was a problem hiding this comment.
I ran the focused response and length suites on 31cac38 with Node 24.22.1 on Windows: 73 tests passed. I also checked two neighboring public request paths: a HEAD response with ctx.type = "json"; ctx.body = null returns 200 and Content-Length: 4 without a body, while a GET with JSON undefined returns 200, length 4, and null. The _explicitStatus guard also leaves an explicitly chosen status intact. This covers the status and length behavior across GET and HEAD.
Checklist
When the response type is JSON and the body is set to
nullorundefined, Koa sendsnull, but that branch ofset bodyreturns before the usual status and length handling.ctx.type = 'json'; ctx.body = nullresponds404 Not Foundwith bodynull.ctx.body = 'hello world'; ctx.type = 'json'; ctx.body = nullkeepsContent-Length: 11while writing 4 bytes, so the client waits for bytes that never come.The branch now does what the string branch does: it sets the status to 200 unless one was set explicitly, and sets Content-Length to the length of
null. An explicit status such asctx.status = 404is kept. Apps that set the JSON type first and then assign a null result will get 200 instead of 404. #2014 changes the same lines (stream cleanup only), so whichever lands second needs a small rebase.npm run lint,npm run build && npm test(470 passing),npm run test:coverage.expected 200 "OK", got 404 "Not Found",aborted,11 !== 4). All 4 pass with this change.Summary by Sourcery
Correct response handling for JSON null bodies so status and Content-Length remain consistent with the returned payload.
Bug Fixes:
Tests:
Summary by CodeRabbit
nullbody now return the valid JSON valuenull, with the correct content length.