t3code icon indicating copy to clipboard operation
t3code copied to clipboard

fix(shared): fix deepMerge data-corruption bugs and add tests

Open kridaydave opened this issue 4 months ago • 3 comments

Fixes two deepMerge bugs: it destroyed Date/Map/class-instance values (prototype loss) and silently discarded the entire base on a primitive patch.

  • Only recurse into plain objects now; Date/Map/instances are overwritten whole.
  • Throw on a primitive patch instead of dropping the base (null still allowed).
  • Added regression tests in Struct.test.ts and Struct.edgeCases.test.ts.

All gates green (typecheck, check, test).


[!NOTE] Medium Risk Shared utility behavior change: callers that passed a non-null primitive or array as the top-level patch will now throw instead of getting a silent wrong result; merge semantics for non-plain nested values also change.

Overview deepMerge in Struct.ts now recurses only when both sides are plain objects (Object.prototype), so Date, Map, class instances, arrays, and functions are replaced wholesale instead of being walked like nested records (which broke Date prototypes and similar values).

Invalid top-level patches throw instead of silently returning a primitive and dropping the base object; null at the top level is still allowed.

New Struct.test.ts and Struct.edgeCases.test.ts lock in normal merge behavior and document remaining quirks (array overwrite, null not skipped, shallow copy of nested refs).

Reviewed by Cursor Bugbot for commit 3405a012d347f52cc7cf79dc7d9489c16c6bee3c. Bugbot is set up for automated code reviews on this repo. Configure here.

[!NOTE]

Fix data-corruption bugs in deepMerge and add test coverage

  • Introduces isPlainObject in Struct.ts to restrict recursion to plain objects; dates, maps, and class instances are now overwritten rather than recursively merged.
  • deepMerge now throws when the top-level patch is a non-null primitive (e.g. number, array, function) instead of silently returning it.
  • Adds primary tests in Struct.test.ts and edge-case tests in Struct.edgeCases.test.ts documenting both corrected and remaining lossy behaviors.
  • Behavioral Change: callers passing a primitive or array as the top-level patch will now receive a thrown error instead of a silent no-op overwrite.

Macroscope summarized 3405a01.

kridaydave avatar Jul 18 '26 19:07 kridaydave

You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard.

[!IMPORTANT]

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 2fc415e1-ed28-47a1-afd7-c60d09002e8f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • [ ] 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

coderabbitai[bot] avatar Jul 18 '26 19:07 coderabbitai[bot]

Approvability

Verdict: Needs human review

This bug fix introduces meaningful runtime behavior changes to a shared deepMerge utility: new exception pathways for primitive patches and changed merge semantics for Date/Map/class instances. These changes to shared infrastructure warrant human review to ensure callers aren't affected.

You can customize Macroscope's approvability policy. Learn more.

macroscopeapp[bot] avatar Jul 18 '26 19:07 macroscopeapp[bot]