Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove false positive in circular structure detection #16380

Merged
merged 8 commits into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
})