fix(clone): isolate cloned arrays from source documents - #16281
Merged
vkarpov15 merged 2 commits intoMay 14, 2026
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes a $clone() isolation bug where cloned Mongoose arrays could still be internally parented to the source document, allowing modifications on the clone (and even clone-time population) to mark the original document as modified. The changes ensure cloned document arrays have correct subdocument indexes after source mutations and that both document arrays and primitive arrays don’t leak modification tracking back to the original.
Changes:
- Restamp cloned document-array subdocument indexes during clone array population (while suppressing change tracking).
- Add regression tests to ensure
$clone()doesn’t mutate the original document’smodifiedPaths()for primitive arrays and doesn’t propagate modifications from clone to original. - Add test coverage for cloning document arrays with
nullentries and for index correctness after source array edits.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| test/document.test.js | Adds regression coverage for clone isolation: subdoc index correctness, null entries in doc arrays, and primitive array modified state isolation. |
| lib/helpers/clone.js | Restamps subdocument indexes during retained document-array cloning while avoiding marking either source or clone modified. |
AbdelrahmanHafez
marked this pull request as ready for review
May 13, 2026 19:22
AbdelrahmanHafez
marked this pull request as draft
May 13, 2026 19:42
AbdelrahmanHafez
force-pushed
the
fix/clone-parent-index-refresh
branch
from
May 13, 2026 19:43
816d14f to
1bc6bd9
Compare
AbdelrahmanHafez
marked this pull request as ready for review
May 13, 2026 19:52
vkarpov15
approved these changes
May 14, 2026
vkarpov15
left a comment
Collaborator
There was a problem hiding this comment.
This is great, thanks 👍
vkarpov15
pushed a commit
that referenced
this pull request
May 14, 2026
* test(document): cover clone array isolation edge cases * fix(clone): refresh cloned document array indexes
Collaborator
|
I also cherry-picked this into 8.x branch. |
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.
re #15958
$clone()was still leaving one parent pointer connected to the source document. We fixed the parent pointers on cloned subdocs, but retained Mongoose arrays were still constructed with the source document as their internal array parent. That meant cloning could mark the original document modified while populating the clone, and later writes to a cloned array could bubblemarkModified()back to the original.This PR: