Skip to content

fix(documentarray): reindex subdocs after reordering#16282

Draft
AbdelrahmanHafez wants to merge 12 commits into
Automattic:masterfrom
AbdelrahmanHafez:fix/document-array-reindex-after-removal
Draft

fix(documentarray): reindex subdocs after reordering#16282
AbdelrahmanHafez wants to merge 12 commits into
Automattic:masterfrom
AbdelrahmanHafez:fix/document-array-reindex-after-removal

Conversation

@AbdelrahmanHafez
Copy link
Copy Markdown
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez commented May 13, 2026

Document array operations that remove or reorder entries can leave surviving subdocs with stale __index values. After operations like pull(), shift(), splice(), $shift(), unshift(), positioned push(), sort(), or reverse(), a subdoc can still think it lives at its previous array index. A later nested change can then mark and save the wrong path, for example addresses.1.city instead of addresses.0.city.

This PR:

  • restamps document-array subdoc indexes after operations that can move existing subdocs
  • keeps append-only push() on the fast path, because the new subdoc is already cast with the correct appended index
  • refreshes the parent populated cache after reorder operations that can change doc.populated('array.refPath') order, including sort() and reverse()
  • makes document-array reverse() register a full-array $set, matching other reorder operations and avoiding stale pending atomics after a prior $push
  • adds regression coverage for wrong nested modified paths, persistence after save, populated-cache order, and reverse atomics

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes document array subdocument index bookkeeping after removals so later nested mutations mark the correct path.

Changes:

  • Reindexes surviving subdocuments after pull(), shift(), and splice().
  • Preserves populated cache updates in the same wrappers.
  • Adds regression coverage for saving after removal and mutating the first remaining subdocument.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
lib/types/documentArray/methods/index.js Adds subdocument index restamping after selected document-array removal operations.
test/types.documentarray.test.js Adds regression tests for pull(), splice(), and shift() reindex behavior.

Comment thread lib/types/documentArray/methods/index.js
@AbdelrahmanHafez AbdelrahmanHafez force-pushed the fix/document-array-reindex-after-removal branch from 14e4e57 to f1f0559 Compare May 13, 2026 19:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

Comment thread lib/types/documentArray/methods/index.js
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

Comment thread lib/types/documentArray/methods/index.js Outdated
Comment thread test/types.documentarray.test.js
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

*/

reverse() {
const ret = _baseReverse.call(this);
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@AbdelrahmanHafez AbdelrahmanHafez changed the title fix(documentarray): reindex subdocs after removal fix(documentarray): reindex subdocs after reordering May 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants