fix(internal): avoid calling toJSON twice in safeStringify

YU
18d ago
+52-13

PR Checklist

  • The proposed commit message follows the contribution guidelines: fix(internal): avoid calling toJSON twice in safeStringify

Bugs / Features

  • Related issue(s) linked — found directly while inspecting the serializer; no existing open issue identified.
  • Tests for the changes have been added.
  • Docs have been added / updated — an implementation comment explains why conversion is left to JSON.stringify; there is no public API change.
  • Changesets have been added.

What is the current behavior?

JSON.stringify calls an object's toJSON(key) before passing its result to the replacer. safeStringify then invokes toJSON again when that result is an object with a toJSON method.

For example, a self-returning toJSON runs twice for one property. If its second invocation throws, safeStringify returns SAFE_STRINGIFY_ERROR rather than valid JSON. The extra invocation also lacks the property-key argument. An object returned by toJSON can likewise have its own method unexpectedly invoked.

What is the new behavior?

Leave toJSON conversion to JSON.stringify, then apply the existing circular-reference checks to its result. The traversal stack and WeakSet handling are preserved.

Regression tests cover a self-returning method being called once with its property key, an object returned by toJSON, and errors genuinely thrown by the initial conversion. Existing circular-reference, shared-reference, Date, and custom-serialization tests remain unchanged.

Notes for reviewers

Validation used Node 24.22.1 and Vitest 3.2.4 against the actual source modules:

  • Before the fix: the serializer suite had 2 failing regression tests and 28 passing tests.
  • After the fix: the complete internal package suite had 131 passing tests and 1 existing skipped test across 6 files. An independent second AI review and test run reproduced that result.
  • Biome 1.9.4 checks passed for both changed TypeScript files.
  • Strict TypeScript 5.8.3 checking passed for those two files.
  • Full internal-package tsc reports a pre-existing Map inference error in src/utils/objects.spec.ts:171. Running the same check with the original HEAD source and tests produced identical output.
  • git diff --check passed.

The full monorepo build, full-repository lint, core integration/e2e tests, and Node 20/22 matrix were not run locally. Tools were installed in an isolated lightweight environment rather than installing the entire monorepo.

Historical PR #961 replaced recursive reconstruction with stack-based traversal for performance. This patch keeps that approach; its existing custom-serialization tests compare against native JSON.stringify. The behavioral correction is specifically removal of the extra conversion, including for objects returned by toJSON.

This contribution was prepared and independently checked with AI assistance. The checks listed above were actually run; this does not claim human review.

Independent validation refresh (2026-11-03)

Exact PR head de8bb6eb2173790c1d736e19fd3ba052f5999e67 was rechecked against unchanged main 72a46c76b4507e1f98b41d5050a92bd30587eb70. A fresh Node 24 run of all six internal-package test files produced 129 passed, 2 failed and 1 skipped on the baseline, then 131 passed and 1 skipped on the PR. This repeats the package inventory above; it is not an additional 131 unique tests. Ten separate direct-TypeScript boundary probes produced 7 passed/3 failed on baseline and 10 passed on the PR. Changed-file strict TypeScript and Biome checks passed. Whole-package TypeScript still reports the identical pre-existing TS2769 Map inference diagnostic on baseline and PR; it did not pass.

This refresh did not run the full workspace build/tests/lint, core integration/E2E, or Node 20/22. It does not claim hosted CI success: the PR workflow currently awaits maintainer approval, and CodeRabbit skipped review while the PR was a draft. The independent verification was AI-assisted, not human or maintainer approval.

Summary by CodeRabbit

  • Bug Fixes
    • Safe serialization now respects toJSON behavior without invoking it twice, and handles circular references and serialization errors correctly.
  • Tests
    • Added coverage for toJSON call counts, returned values, and errors.