Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ export const cloneDataFromOriginalDoc = (
): JsonArray | JsonObject => {
if (Array.isArray(originalDocData)) {
return originalDocData.map((row) => {
// Recurse for nested arrays so they stay arrays instead of being
// spread into index-keyed objects (`{...[1, 2]}` -> `{ 0: 1, 1: 2 }`).
if (Array.isArray(row)) {
return cloneDataFromOriginalDoc(row)
}

if (typeof row === 'object' && row != null) {
return {
...row,
Expand Down
30 changes: 30 additions & 0 deletions test/fields/int.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4318,6 +4318,36 @@ describe('Fields', () => {
expect(updatedJsonFieldsDoc.json.state).toEqual({})
})

it('should preserve nested arrays in a json field omitted from a partial update', async () => {
const arrayOfArrays = [
[1, 2],
[3, 4],
]

const jsonFieldsDoc = await payload.create({
collection: 'json-fields',
data: {
customJSON: arrayOfArrays,
},
})

expect(jsonFieldsDoc.customJSON).toStrictEqual(arrayOfArrays)

// Partial update that intentionally omits `customJSON` so that its
// existing value is cloned from the original doc via
// `cloneDataFromOriginalDoc`. Nested arrays must survive that clone
// instead of being turned into index-keyed objects.
const updatedJsonFieldsDoc = await payload.update({
id: jsonFieldsDoc.id,
collection: 'json-fields',
data: {
json: { foo: 'bar' },
},
})

expect(updatedJsonFieldsDoc.customJSON).toStrictEqual(arrayOfArrays)
})

describe('querying', () => {
let fooBar
let bazBar
Expand Down
Loading