-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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(document): handle calling set()
underneath a strict: false subdocument path from the top-level document
#13339
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
127ccd8
fix(document): handle calling `set()` underneath a strict: false subd…
vkarpov15 0f37569
style: quick reformat
vkarpov15 2605339
fix: recursively propagate strict values to child schemas, add docs t…
vkarpov15 ac00359
docs: quick jsdoc fix
vkarpov15 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Find the deepest subdocument along a given path to ensure setter functions run | ||
* with the correct subdocument as `this`. If no subdocuments, returns the top-level | ||
* document. | ||
* | ||
* @param {Document} doc | ||
* @param {String[]} parts | ||
* @param {Schema} schema | ||
* @returns Document | ||
*/ | ||
|
||
module.exports = function getDeepestSubdocumentForPath(doc, parts, schema) { | ||
vkarpov15 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let curPath = parts[0]; | ||
let curSchema = schema; | ||
let subdoc = doc; | ||
for (let i = 0; i < parts.length - 1; ++i) { | ||
const curSchemaType = curSchema.path(curPath); | ||
if (curSchemaType && curSchemaType.schema) { | ||
let newSubdoc = subdoc.get(curPath); | ||
curSchema = curSchemaType.schema; | ||
curPath = parts[i + 1]; | ||
if (Array.isArray(newSubdoc) && !isNaN(curPath)) { | ||
newSubdoc = newSubdoc[curPath]; | ||
curPath = ''; | ||
} | ||
if (newSubdoc == null) { | ||
break; | ||
} | ||
subdoc = newSubdoc; | ||
} else { | ||
curPath += curPath.length ? '.' + parts[i + 1] : parts[i + 1]; | ||
} | ||
} | ||
|
||
return subdoc; | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
/** | ||
* Find the `strict` mode setting for the deepest subdocument along a given path | ||
* to ensure we have the correct default value for `strict`. When setting values | ||
* underneath a subdocument, we should use the subdocument's `strict` setting by | ||
* default, not the top-level document's. | ||
* | ||
* @param {Schema} schema | ||
* @param {String[]} parts | ||
* @returns {boolean | 'throw' | undefined} | ||
*/ | ||
|
||
module.exports = function getSubdocumentStrictValue(schema, parts) { | ||
if (parts.length === 1) { | ||
return undefined; | ||
} | ||
let cur = parts[0]; | ||
let strict = undefined; | ||
for (let i = 0; i < parts.length - 1; ++i) { | ||
const curSchemaType = schema.path(cur); | ||
if (curSchemaType && curSchemaType.schema) { | ||
strict = curSchemaType.schema.options.strict; | ||
schema = curSchemaType.schema; | ||
cur = curSchemaType.$isMongooseDocumentArray && !isNaN(parts[i + 1]) ? '' : parts[i + 1]; | ||
} else { | ||
cur += cur.length ? ('.' + parts[i + 1]) : parts[i + 1]; | ||
} | ||
} | ||
|
||
return strict; | ||
}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const getDeepestSubdocumentForPath = require('../../lib/helpers/document/getDeepestSubdocumentForPath'); | ||
const { mongoose } = require('../common'); | ||
|
||
describe('getDeepestSubdocumentForPath', function() { | ||
beforeEach(() => mongoose.deleteModel(/Test/)); | ||
after(() => mongoose.deleteModel(/Test/)); | ||
|
||
it('returns top-level document if no subdocs', function() { | ||
const Test = mongoose.model('Test', mongoose.Schema({ name: String })); | ||
const doc = new Test({ name: 'foo' }); | ||
|
||
assert.strictEqual(getDeepestSubdocumentForPath(doc, ['name'], doc.schema), doc); | ||
}); | ||
|
||
it('picks up single nested subdocs along the path', function() { | ||
const Test = mongoose.model('Test', mongoose.Schema({ | ||
name: String, | ||
nested: mongoose.Schema({ | ||
nestedPath: String | ||
}) | ||
})); | ||
const doc = new Test({ name: 'foo', nested: { nestedPath: 'bar' } }); | ||
|
||
assert.strictEqual( | ||
getDeepestSubdocumentForPath(doc, ['nested', 'nestedPath'], doc.schema), | ||
doc.nested | ||
); | ||
}); | ||
|
||
it('picks up document arrays', function() { | ||
const Test = mongoose.model('Test', mongoose.Schema({ | ||
name: String, | ||
docArr: [{ | ||
nestedPath: String | ||
}] | ||
})); | ||
const doc = new Test({ name: 'foo', docArr: [{ nestedPath: 'bar' }] }); | ||
|
||
const res = getDeepestSubdocumentForPath(doc, ['docArr', '0', 'nestedPath'], doc.schema); | ||
const expected = doc.docArr[0]; | ||
assert.strictEqual(res, expected); | ||
}); | ||
|
||
it('picks up doubly nested subdocuments', function() { | ||
const Test = mongoose.model('Test', mongoose.Schema({ | ||
name: String, | ||
l1: mongoose.Schema({ | ||
l2: mongoose.Schema({ | ||
nested: String | ||
}) | ||
}) | ||
})); | ||
const doc = new Test({ name: 'foo', l1: { l2: { nested: 'bar' } } }); | ||
|
||
const res = getDeepestSubdocumentForPath(doc, ['l1', 'l2', 'nested'], doc.schema); | ||
const expected = doc.l1.l2; | ||
assert.strictEqual(res, expected); | ||
}); | ||
|
||
it('returns deepest non-null subdoc', function() { | ||
const Test = mongoose.model('Test', mongoose.Schema({ | ||
name: String, | ||
l1: mongoose.Schema({ | ||
l2: mongoose.Schema({ | ||
nested: String | ||
}) | ||
}) | ||
})); | ||
const doc = new Test({ name: 'foo', l1: { l2: null } }); | ||
|
||
const res = getDeepestSubdocumentForPath(doc, ['l1', 'l2', 'nested'], doc.schema); | ||
const expected = doc.l1; | ||
assert.strictEqual(res, expected); | ||
}); | ||
|
||
it('picks up single nested subdocs under document arrays', function() { | ||
const Test = mongoose.model('Test', mongoose.Schema({ | ||
name: String, | ||
docArr: [{ | ||
nested: mongoose.Schema({ | ||
l3Path: String | ||
}) | ||
}] | ||
})); | ||
const doc = new Test({ name: 'foo', docArr: [{ nested: { l3Path: 'bar' } }] }); | ||
|
||
const res = getDeepestSubdocumentForPath(doc, ['docArr', '0', 'nested', 'l3Path'], doc.schema); | ||
const expected = doc.docArr[0].nested; | ||
assert.strictEqual(res, expected); | ||
}); | ||
}); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Undoing this change fixes #13876