fix(json-sorter): __proto__ key disappears from the output #34

Open
opened 2026-10-28 21:52:10 +00:00 by flopana · 0 comments
Owner

Part of #30

Branch: feature/improve-json-sorter

Bug

sortObjKeys in JsonSorter.tsx assigns sortedObj[key] = .... For the key __proto__ this sets the prototype, so JSON.stringify drops the key.

Input Output now
{"b":1,"__proto__":{"x":1}} {"b":1}

Verification: the review agent checked the fix in node. The bug follows from how JS assigns __proto__.

Red test

Add src/lib/jsonSorter.test.ts. The input above keeps the __proto__ key. Also cover nested objects, arrays and primitive roots.

Fix

  • Move sortObjKeys and JsonValue to src/lib/jsonSorter.ts.
  • Build the object with Object.fromEntries.
  • Delete the dead JSON.stringify(obj, null, 2) validity check.

Known limit, no fix

JS objects put integer-like keys first. {"b":1,"10":2,"2":3} gives {"2":3,"10":2,"b":1}.

Part of #30 Branch: `feature/improve-json-sorter` ## Bug `sortObjKeys` in `JsonSorter.tsx` assigns `sortedObj[key] = ...`. For the key `__proto__` this sets the prototype, so `JSON.stringify` drops the key. | Input | Output now | |---|---| | `{"b":1,"__proto__":{"x":1}}` | `{"b":1}` | Verification: the review agent checked the fix in node. The bug follows from how JS assigns `__proto__`. ## Red test Add `src/lib/jsonSorter.test.ts`. The input above keeps the `__proto__` key. Also cover nested objects, arrays and primitive roots. ## Fix - Move `sortObjKeys` and `JsonValue` to `src/lib/jsonSorter.ts`. - Build the object with `Object.fromEntries`. - Delete the dead `JSON.stringify(obj, null, 2)` validity check. ## Known limit, no fix JS objects put integer-like keys first. `{"b":1,"10":2,"2":3}` gives `{"2":3,"10":2,"b":1}`.
Sign in to join this conversation.
No description provided.