[PR #5975] fix(jsonc): preserve numeric precision for large integers in JSON body #5445

Open
opened 2026-03-17 02:53:08 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/hoppscotch/hoppscotch/pull/5975
Author: @VivekNeer
Created: 3/11/2026
Status: 🔄 Open

Base: mainHead: fix/preserve-numeric-precision-json-body


📝 Commits (1)

  • 4e97106 fix(jsonc): preserve numeric precision for large integers in JSON body

📊 Changes

4 files changed (+142 additions, -12 deletions)

View changed files

📝 packages/hoppscotch-cli/src/__tests__/unit/jsonc.spec.ts (+57 -0)
📝 packages/hoppscotch-cli/src/utils/jsonc.ts (+9 -6)
packages/hoppscotch-common/src/helpers/__tests__/jsonc.spec.ts (+67 -0)
📝 packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts (+9 -6)

📄 Description

Closes #5970

JSON request bodies containing integers larger than Number.MAX_SAFE_INTEGER (> 9007199254740991) were being silently truncated when sent. For example, {"id": 9007199254740993} would arrive at the server as {"id": 9007199254740992} — last digit flipped with no warning.

What's changed

  • packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts — Instead of JSON.stringify(node.value) for number nodes (which already lost precision at JS parse time via IEEE-754), the fix slices the original source string using the AST node's offset and length, which are always intact. The originalText parameter is threaded through all recursive calls in convertNodeToJSON.
  • packages/hoppscotch-cli/src/utils/jsonc.ts — Same fix applied to the CLI's identical copy of the same utility.
  • packages/hoppscotch-common/src/helpers/__tests__/jsonc.spec.ts — New test file with 10 precision tests (boundary values, nested objects, arrays, combined with comment/trailing-comma stripping, negative integers, small integer regression).
  • packages/hoppscotch-cli/src/__tests__/unit/jsonc.spec.ts — New describe block with 6 precision tests for the CLI copy.

Notes to reviewers

The root cause is in the "number" case of convertNodeToJSON:

// Before — node.value is already a JS number, precision lost at parse time
case "number":
  return JSON.stringify(node.value)

// After — slice the raw source string, which is always intact
case "number":
  return originalText.slice(node.offset, node.offset + node.length)

jsonc-parser's parseTree() stores number tokens as JS number primitives, so values beyond 2^53 − 1 are truncated before JSON.stringify even runs. Using offset/length to slice the original text sidesteps the IEEE-754 issue entirely.

The same bug exists independently in the CLI package — both copies are fixed here.

Full test suite in hoppscotch-common (50 files / 682 tests) passes unchanged after the fix. ><


Summary by cubic

Preserves full precision for large integers in JSONC request bodies by serializing number tokens from the original source text. Prevents silent rounding (e.g., 9007199254740993) in both the app and hoppscotch-cli.

  • Bug Fixes
    • In hoppscotch-common and hoppscotch-cli jsonc helpers, convertNodeToJSON() now slices originalText for "number" nodes using AST offset/length, and threads originalText through array/object/property cases and the entry call.
    • Tests: added a new suite in hoppscotch-common and expanded CLI tests to cover objects/arrays, comments/trailing commas, Number.MAX_SAFE_INTEGER bounds, negatives, and verbatim floats; CLI suite aligned with the common one.

Written for commit 4e9710672c. Summary will update on new commits.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/hoppscotch/hoppscotch/pull/5975 **Author:** [@VivekNeer](https://github.com/VivekNeer) **Created:** 3/11/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/preserve-numeric-precision-json-body` --- ### 📝 Commits (1) - [`4e97106`](https://github.com/hoppscotch/hoppscotch/commit/4e9710672c98ff18034ff3b08c30b0fe92099a93) fix(jsonc): preserve numeric precision for large integers in JSON body ### 📊 Changes **4 files changed** (+142 additions, -12 deletions) <details> <summary>View changed files</summary> 📝 `packages/hoppscotch-cli/src/__tests__/unit/jsonc.spec.ts` (+57 -0) 📝 `packages/hoppscotch-cli/src/utils/jsonc.ts` (+9 -6) ➕ `packages/hoppscotch-common/src/helpers/__tests__/jsonc.spec.ts` (+67 -0) 📝 `packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts` (+9 -6) </details> ### 📄 Description Closes #5970 JSON request bodies containing integers larger than `Number.MAX_SAFE_INTEGER` (> `9007199254740991`) were being silently truncated when sent. For example, `{"id": 9007199254740993}` would arrive at the server as `{"id": 9007199254740992}` — last digit flipped with no warning. ### What's changed - **`packages/hoppscotch-common/src/helpers/editor/linting/jsonc.ts`** — Instead of `JSON.stringify(node.value)` for number nodes (which already lost precision at JS parse time via IEEE-754), the fix slices the original source string using the AST node's `offset` and `length`, which are always intact. The `originalText` parameter is threaded through all recursive calls in `convertNodeToJSON`. - **`packages/hoppscotch-cli/src/utils/jsonc.ts`** — Same fix applied to the CLI's identical copy of the same utility. - **`packages/hoppscotch-common/src/helpers/__tests__/jsonc.spec.ts`** — New test file with 10 precision tests (boundary values, nested objects, arrays, combined with comment/trailing-comma stripping, negative integers, small integer regression). - **`packages/hoppscotch-cli/src/__tests__/unit/jsonc.spec.ts`** — New `describe` block with 6 precision tests for the CLI copy. ### Notes to reviewers The root cause is in the `"number"` case of `convertNodeToJSON`: ```ts // Before — node.value is already a JS number, precision lost at parse time case "number": return JSON.stringify(node.value) // After — slice the raw source string, which is always intact case "number": return originalText.slice(node.offset, node.offset + node.length) ``` `jsonc-parser`'s `parseTree()` stores number tokens as JS `number` primitives, so values beyond `2^53 − 1` are truncated before `JSON.stringify` even runs. Using `offset`/`length` to slice the original text sidesteps the IEEE-754 issue entirely. The same bug exists independently in the CLI package — both copies are fixed here. Full test suite in `hoppscotch-common` (50 files / 682 tests) passes unchanged after the fix. >< <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Preserves full precision for large integers in JSONC request bodies by serializing number tokens from the original source text. Prevents silent rounding (e.g., 9007199254740993) in both the app and `hoppscotch-cli`. - **Bug Fixes** - In `hoppscotch-common` and `hoppscotch-cli` `jsonc` helpers, `convertNodeToJSON()` now slices `originalText` for `"number"` nodes using AST `offset`/`length`, and threads `originalText` through array/object/property cases and the entry call. - Tests: added a new suite in `hoppscotch-common` and expanded CLI tests to cover objects/arrays, comments/trailing commas, `Number.MAX_SAFE_INTEGER` bounds, negatives, and verbatim floats; CLI suite aligned with the common one. <sup>Written for commit 4e9710672c98ff18034ff3b08c30b0fe92099a93. Summary will update on new commits.</sup> <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hoppscotch#5445
No description provided.