fix: preserve nested arrays when cloning omitted json field data - #17713
Open
vjymisal0 wants to merge 1 commit into
Open
fix: preserve nested arrays when cloning omitted json field data#17713vjymisal0 wants to merge 1 commit into
vjymisal0 wants to merge 1 commit into
Conversation
…data
cloneDataFromOriginalDoc shallow-cloned every array row with `{...row}`,
which is correct for plain objects but silently turns nested arrays into
index-keyed objects (`[1, 2]` -> `{ "0": 1, "1": 2 }`). This runs whenever
a partial update omits a field and its previous value is carried forward
via getFallbackValue, so a json field storing an array-of-arrays (e.g.
coordinate tuples) gets corrupted on any update that doesn't touch it.
Draft saves skip validation entirely, so the corrupted shape persists.
Recurse into nested arrays instead of spreading them as objects, so array
shape is preserved at any depth while existing object-cloning behavior is
unchanged.
Fixes payloadcms#17475
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What?
cloneDataFromOriginalDocmangles a JSON field's array-of-arrays value into index-keyed objects when the field is omitted from a partial update.Why?
Fixes #17475
On a partial
payload.update/ RESTPATCHthat omits ajsonfield,getFallbackValuecarries the existing value forward viacloneDataFromOriginalDoc(packages/payload/src/fields/hooks/beforeChange/cloneDataFromOriginalDoc.ts). That function shallow-clones each array row with{...row}.typeofan array is'object', so every nested array row (e.g.[1, 2]) gets spread into{ "0": 1, "1": 2 }instead of staying an array. Draft saves skip validation, so the corrupted shape can persist straight to the database; downstream consumers iterating the tuples then throwTypeError: object is not iterable.How?
In the array branch of
cloneDataFromOriginalDoc, checkArray.isArray(row)first and recurse intocloneDataFromOriginalDocfor that row instead of falling through to the{...row}object-spread branch. This preserves array shape at any nesting depth while leaving the existing shallow-clone behavior for plain objects untouched.Test plan
Added an integration test in
test/fields/int.spec.ts(jsondescribe block) that creates ajson-fieldsdoc with an array-of-arrays value in the schema-freecustomJSONfield, performs a partial update that omitscustomJSON, and asserts the array structure is preserved on the returned doc.Note on local verification: I was not able to get a full
pnpm installto complete in my environment (network timeouts fetching registry packages partway through a ~9 minute resolve), so I could not runpnpm test:intagainst this change locally. The fix and test are written to match the existing code's structure and conventions; I'm flagging this explicitly rather than claiming a green run I didn't get. Happy to iterate based on CI results.