Skip to content

Commit

Permalink
Remove false positive in circular structure detection (#16380)
Browse files Browse the repository at this point in the history
Co-authored-by: Joe Haddad <joe.haddad@zeit.co>
  • Loading branch information
m-lautenbach and Timer committed Sep 12, 2020
1 parent 71c5ab6 commit 76e86e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 8 additions & 3 deletions packages/next/lib/is-serializable-props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,16 @@ 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) =>
isSerializable(newRefs, nestedValue, `${path}[${index}]`)
)
value.every((nestedValue, index) => {
if (seen.has(nestedValue)) {
return true
}
seen.add(nestedValue)
return isSerializable(newRefs, nestedValue, `${path}[${index}]`)
})
) {
return true
}
Expand Down
9 changes: 9 additions & 0 deletions test/unit/is-serializable-props.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,13 @@ Reason: Circular references cannot be expressed in JSON (references: \`.k\`)."
})
).toBe(true)
})

it('allows identical object instances in an array', () => {
const obj = { foo: 'bar' }
const arr = [obj, obj]
const objWithArr = { deep: { arr } }

expect(isSerializableProps('/', 'test', { arr })).toBe(true)
expect(isSerializableProps('/', 'test', { objWithArr })).toBe(true)
})
})

0 comments on commit 76e86e0

Please sign in to comment.