fix(utils): Handle toJSON
methods that return circular references
#5323
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.
This PR handles a case in
normalize()
when a.toJSON
method on an input value either returns an object with a circular reference or the input value itself, causing problems down the line.Changes
toJSON
Compromise
To detect circular references we need to do a memoize call before normalizing the return value from
toJSON
. However, at the location we're currently calling thetoJSON
method,value
could still be a primitive likestring
ornumber
and we can't callmemoize
with primitives because the underlyingWeakSet
only takesobject
s.As a compromise, we're moving the
toJSON
call down to wherevalue
is certainly an object or array and after the circ-ref-check. I believe it is ok for us to only calltoJSON
on values that are non-primitives since you can't even set a field on strings, numbers, symbols, etc. unless you modify their prototype.If somebody has a better idea on how to tackle this best, feel free to make suggestions! - I myself am not too happy with this solution.