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

fix(schema): make Schema.prototype.clone() avoid creating different copies of subdocuments and single nested paths underneath single nested paths #13671

Merged
merged 1 commit into from Aug 1, 2023

Conversation

vkarpov15
Copy link
Collaborator

Re: #13626

Summary

#13626 is one of those issues where doing the "5 whys" exercise takes you down a very deep rabbit hole. There are a few issues that came together to form the issue reported in #13626. The one addressed in this PR is that Schema.prototype.clone() unintentionally creates duplicate constructors for subdocuments and document arrays that are nested underneath a subdocument path.

new Schema({
  docArr1: [{ name: String }] // unaffected
  subdoc1: new Schema({
    docArr2: [{ name: String }], // affected
    subdoc2: new Schema({ name: String }) // affected
  });
}).clone();

The issue is that Schema.prototype.clone() first uses utils.clone() to clone the schema's paths, and then clone the single nested paths:

s.paths = utils.clone(this.paths);
s.singleNestedPaths = utils.clone(this.singleNestedPaths);

clone() calls SchemaType.prototype.clone(), on each schema type it finds. This means that, after cloning singleNestedPaths, paths['subdoc1'].schema.paths['subdoc2'] will not be the same as singleNestedPaths['subdoc1.subdoc2']. They will be distinct clones of the original schema's singleNestedPaths['subdoc1.subdoc2'].

Fix is to re-assemble singleNestedPaths from the cloned schemas' paths.

Examples

… copies of subdocuments and single nested paths underneath single nested paths
@hasezoey hasezoey added this to the 6.11.5 milestone Jul 29, 2023
Copy link
Collaborator

@Uzlopak Uzlopak left a comment

Choose a reason for hiding this comment

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

LGTM

@vkarpov15 vkarpov15 merged commit 622fa1c into 6.x Aug 1, 2023
34 checks passed
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-13626 branch August 1, 2023 14:41
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.

None yet

4 participants