fix(schema): make Schema.prototype.clone()
avoid creating different copies of subdocuments and single nested paths underneath single nested paths
#13671
+34
−1
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: #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.The issue is that
Schema.prototype.clone()
first usesutils.clone()
to clone the schema's paths, and then clone the single nested paths:clone()
callsSchemaType.prototype.clone()
, on each schema type it finds. This means that, after cloningsingleNestedPaths
,paths['subdoc1'].schema.paths['subdoc2']
will not be the same assingleNestedPaths['subdoc1.subdoc2']
. They will be distinct clones of the original schema'ssingleNestedPaths['subdoc1.subdoc2']
.Fix is to re-assemble
singleNestedPaths
from the cloned schemas'paths
.Examples