fix: enforce maximumDepth for nested objects with array replacers#64
Open
Chamath-Adithya wants to merge 1 commit into
Open
fix: enforce maximumDepth for nested objects with array replacers#64Chamath-Adithya wants to merge 1 commit into
Chamath-Adithya wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request: Enforce
maximumDepthfor nested objects in array replacersDescription
This pull request addresses a bug in the array-replacer-based stringification path (
stringifyArrayReplacer). Currently, themaximumDepthoption is checked and enforced correctly when serializing arrays, but the check is completely bypassed when traversing nested objects. This allows nested objects to serialize beyond the configuredmaximumDepthboundary and potentially trigger stack overflows on heavily nested structures.With this fix,
stringifyArrayReplacercorrectly enforces themaximumDepthconstraint on nested objects, ensuring consistent behavior with other stringifier paths (stringifySimple,stringifyFnReplacer, andstringifyIndent).Solution
Inside
stringifyArrayReplacer(inindex.js), added themaximumDepthcomparison block right before pushing an object onto the traversal stack:This perfectly mirrors the boundary check implemented in the other serialization paths.
Verification & Testing
1. New/Updated Tests
res2(which testsmaximumDepth: 2in combination with an array replacer) intest.jsto correctly expect'{"a":{"a":"[Array]","b":"[Object]"}}'instead of{}.maximumDepth: 1in combination with an array replacer, verifying that it correctly truncates to'{"a":"[Object]"}'.2. Standard Compliance & Style
Ran the tap tests and standard linter successfully:
npm test npm run lintAll 74,937 assertions passed with 100% code coverage.