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. |
|
Checking in — @magion33 (issue reporter) confirmed the fix. Note that duplicate PRs #577 and #578 targeting the same #571 issue were opened and closed this week; this PR (#572) is the original, has test coverage, and keeps the scope narrow. @pi0 would appreciate a final sign-off when you have a moment! |
|
Friendly week-21 nudge. Just checking if there is anything I can adjust on this one. The change is two lines in |
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