Unverified Commit bbde18f1 authored by tolfino's avatar tolfino Committed by GitHub

Fix false positive in isSerializable with shared references in arrays (#18232)

This replaces the seen set with a newly instantiated map of refs per
value in the array. This corrects the behavior that the refs map passed
into isSerializable contains only values along the path to the current
value: previously, because the refs map was shared among all values in
the array, this would trigger on instances such as:

```js
    const x = [];
    isSerializableProps('/', 'test', { arr: [x, [x]] });
```

... where the reference is shared but there is no cycle formed, as the
presence of elements is disjoint.

Fixes #18228.
parent 91d4aa54
......@@ -98,14 +98,9 @@ export function isSerializableProps(
if (Array.isArray(value)) {
visit(refs, value, path)
const seen = new Set<any>()
const newRefs = new Map(refs)
if (
value.every((nestedValue, index) => {
if (seen.has(nestedValue)) {
return true
}
seen.add(nestedValue)
const newRefs = new Map(refs)
return isSerializable(newRefs, nestedValue, `${path}[${index}]`)
})
) {
......
......@@ -284,4 +284,13 @@ Reason: Circular references cannot be expressed in JSON (references: \`.k\`)."
expect(isSerializableProps('/', 'test', { arr })).toBe(true)
expect(isSerializableProps('/', 'test', { objWithArr })).toBe(true)
})
it('allows identical object instances in an array deeply', () => {
const obj = { foo: 'bar' }
const arr = [obj, [obj]]
const objWithArr = { deep: { arr } }
expect(isSerializableProps('/', 'test', { arr })).toBe(true)
expect(isSerializableProps('/', 'test', { objWithArr })).toBe(true)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment