fix(utils): move instanceof guards before value.buffer in isJSONSerializable (#580)#590
fix(utils): move instanceof guards before value.buffer in isJSONSerializable (#580)#590MohammadYusif wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✅ Files skipped from review due to trivial changes (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe isJSONSerializable checks were reordered so FormData/URLSearchParams instanceof exclusions run before accessing value.buffer. A Vitest suite was added to assert expected true/false results across FormData, URLSearchParams, ArrayBuffer, Uint8Array, plain objects/arrays, objects with toJSON, and undefined. ChangesJSON Serialization Safety and Testing
🎯 2 (Simple) | ⏱️ ~10 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
src/utils.ts (1)
530-572: ⚡ Quick winConsider adding tests for primitive types for completeness.
The test suite thoroughly validates the fix for issue
#580and covers the main object/buffer-backed types. For complete coverage of the function's contract (lines 22-24 insrc/utils.ts), consider adding test cases for primitive types that are JSON-serializable:null, strings, numbers, and booleans.✅ Suggested additional test cases
it("returns true for null", () => { expect(isJSONSerializable(null)).toBe(true); }); it("returns true for strings", () => { expect(isJSONSerializable("hello")).toBe(true); }); it("returns true for numbers", () => { expect(isJSONSerializable(42)).toBe(true); }); it("returns true for booleans", () => { expect(isJSONSerializable(true)).toBe(true); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@src/utils.ts` around lines 530 - 572, Add unit tests for primitive JSON-serializable values to cover the isJSONSerializable contract in src/utils.ts: create tests that call isJSONSerializable with null, a string (e.g., "hello"), a number (e.g., 42), and a boolean (e.g., true) and assert they return true; place these alongside the existing tests for object/buffer-backed types so the suite verifies both object and primitive branches of isJSONSerializable.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@src/utils.ts`:
- Around line 530-572: Add unit tests for primitive JSON-serializable values to
cover the isJSONSerializable contract in src/utils.ts: create tests that call
isJSONSerializable with null, a string (e.g., "hello"), a number (e.g., 42), and
a boolean (e.g., true) and assert they return true; place these alongside the
existing tests for object/buffer-backed types so the suite verifies both object
and primitive branches of isJSONSerializable.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: eabf71f1-7319-4bd6-9efe-eb219075c3d7
📒 Files selected for processing (2)
src/utils.tstest/index.test.ts
…lizable (unjs#580) The `instanceof FormData` and `instanceof URLSearchParams` guards were placed after `value.buffer`, meaning any object that happens to have a `buffer` property would be rejected before the explicit type-based exclusion could run. More critically, accessing `value.buffer` on certain host objects can throw before reaching those guards. Moving the `instanceof` checks first ensures that `FormData` and `URLSearchParams` instances are always rejected early, regardless of whether they expose a `buffer` property, and avoids any risk of a thrown accessor on non-plain objects. Also adds direct unit tests for `isJSONSerializable` covering FormData, URLSearchParams (both empty and non-empty), TypedArray, plain objects, arrays, objects with toJSON, and undefined.
3b369fb to
5c988e7
Compare
Summary
isJSONSerializablecheckedvalue.bufferbeforeinstanceof FormData || instanceof URLSearchParams, making those guards unreachable for objects that expose a.bufferpropertyFormDatahas a non-standardtoJSONmethod; if it exposesbuffer, the explicit exclusion would silently failvalue.bufferon certain host objects can also throw before reaching the instanceof checksChanges
src/utils.ts: movedinstanceof FormData || instanceof URLSearchParamsbeforevalue.buffercheck to ensure correct early rejection regardless of internal structuretest/index.test.ts: added 10 unit tests forisJSONSerializablecovering FormData, URLSearchParams, ArrayBuffer, TypedArray, plain objects, arrays, toJSON objects, and undefinedFixes #580
Summary by CodeRabbit
Tests
Refactor