fix: correct isJSONSerializable to handle null without throwing#572
fix: correct isJSONSerializable to handle null without throwing#572EduardF1 wants to merge 1 commit into
Conversation
|
@EduardF1 I'm the creator of the issue and this is exactly what fix I had in mind. LGTM Note: Another partially related thing is that the body serialization logic of the fetch here doesn't allow to have a body of |
|
@magion33 thanks for the LGTM, and good catch on the body-serialization gap. Looking at https://github.com/unjs/ofetch/blob/main/src/fetch.ts#L130: if (context.options.body && isPayloadMethod(context.options.method)) {The leading I'd prefer to keep this PR narrowly focused on the dead-code/throws-on-null bug so it's easy for maintainers to review and merge, and then land the body-serialization fix as a follow-up that references this one. Happy to open that follow-up PR right after this merges (or in parallel if a maintainer signals it's fine to bundle). |
|
@EduardF1 Yes, I agree. It's a different topic, just slightly related. |
SummaryFixes Status
@pi0 happy to adjust scope or naming if needed. Thanks! |
|
Thanks again @magion33 for validating the fix and for agreeing to keep the body-serialization case separate. I am happy to send that follow-up on its own after this one lands if maintainers want it. |
|
Hi team, checking in on this PR! Let me know if you have any questions. |
Problem
isJSONSerializable(null) throws a TypeError and incorrectly returns
alse.
Root cause
Two bugs on the same line:
ull. So === null can never be rue.
ull, execution continues to
alue.buffer, which throws TypeError: Cannot read properties of null (reading 'buffer').
Verified:
ull is valid JSON (JSON.stringify(null) → "null"), so the function should return rue for it.
Fix
Replace === null with
alue === null:
This correctly checks the value itself rather than its ypeof result, so
ull hits the early
eturn true before reaching
alue.buffer.
Fixes #571