Skip to content

fix(logger): correctly serialize shared object references in JSON replacer - #5186

Merged
svozza merged 4 commits into
mainfrom
fix/logger-shared-object-refs
Apr 16, 2026
Merged

svozza merged 4 commits into
mainfrom
fix/logger-shared-object-refs

Conversation

@svozza

@svozza svozza commented Apr 14, 2026

Copy link
Copy Markdown
Contributor

Summary

The Logger's getJsonReplacer() used a WeakSet to detect circular references during JSON serialization, which incorrectly treated duplicate object references as circular. This caused shared objects (e.g. spread array elements) to serialize as null in arrays or be omitted from objects.

Changes

  • Replace WeakSet with ancestor-chain tracking in getJsonReplacer() — only objects on the current path from root to the node being serialized are tracked, allowing the same object to appear in multiple branches while still detecting true circular references
  • Switch from arrow function to regular function in the replacer so JSON.stringify can bind this to the parent object, enabling ancestor-chain pruning
  • Add two test cases reproducing the exact scenarios from the bug report (spread array alongside original, multiple spreads of the same array)

Issue number: closes #5167


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

…lacer

Replace WeakSet with ancestor-chain tracking in getJsonReplacer() to
distinguish duplicate references from true circular references. The
WeakSet remembered every object visited during serialization, causing
shared objects (e.g. spread array elements) to serialize as null.
svozza added 2 commits April 14, 2026 19:22
Capture only the needed values (jsonReplacerFn, logFormatter) in
the closure instead of the entire Logger instance. Resolves
SonarQube S7740 (do not assign this to self).
Resolves SonarQube S7755 (prefer .at() over complex index access).
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Apr 14, 2026
@svozza
svozza requested a review from sdangol April 15, 2026 07:33
Comment thread packages/logger/src/Logger.ts Resolved
@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added size/M PR between 30-99 LOC and removed size/M PR between 30-99 LOC labels Apr 16, 2026
@svozza
svozza merged commit 097e440 into main Apr 16, 2026
52 checks passed
@svozza
svozza deleted the fix/logger-shared-object-refs branch April 16, 2026 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/M PR between 30-99 LOC

2 participants