Skip to content

Commit

Permalink
fix(schema): mark correct path as modified when setting a path undern…
Browse files Browse the repository at this point in the history
…eath a nested array of documents

Fix #8926
  • Loading branch information
vkarpov15 committed May 8, 2020
1 parent 397d812 commit 7911d92
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,10 @@ Schema.prototype.path = function(path, obj) {

// Skip arrays of document arrays
if (_schemaType.$isMongooseDocumentArray) {
_schemaType.$embeddedSchemaType._arrayPath = arrayPath;
_schemaType = _schemaType.$embeddedSchemaType.clone();
} else {
_schemaType.caster._arrayPath = arrayPath;
_schemaType = _schemaType.caster.clone();
}

Expand Down
10 changes: 8 additions & 2 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
* @api private
*/

SchemaArray.prototype.cast = function(value, doc, init) {
SchemaArray.prototype.cast = function(value, doc, init, prev, options) {
// lazy load
MongooseArray || (MongooseArray = require('../types').Array);

Expand Down Expand Up @@ -355,7 +355,13 @@ SchemaArray.prototype.cast = function(value, doc, init) {
if (this.caster.instance === 'Number' && value[i] === void 0) {
throw new MongooseError('Mongoose number arrays disallow storing undefined');
}
value[i] = this.caster.cast(value[i], doc, init);
const opts = {};
if (options != null && options.arrayPath != null) {
opts.arrayPath = options.arrayPath + '.' + i;
} else if (this.caster._arrayPath != null) {
opts.arrayPath = this.caster._arrayPath.slice(0, -2) + '.' + i;
}
value[i] = this.caster.cast(value[i], doc, init, void 0, opts);
}
} catch (e) {
// rethrow
Expand Down
9 changes: 7 additions & 2 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
let selected;
let subdoc;
const _opts = { transform: false, virtuals: false };
options = options || {};

if (!Array.isArray(value)) {
if (!init && !DocumentArrayPath.options.castNonArrays) {
Expand All @@ -374,18 +375,22 @@ DocumentArrayPath.prototype.cast = function(value, doc, init, prev, options) {
if (!!doc && init) {
doc.markModified(this.path);
}
return this.cast([value], doc, init, prev);
return this.cast([value], doc, init, prev, options);
}

if (!(value && value.isMongooseDocumentArray) &&
(!options || !options.skipDocumentArrayCast)) {
!options.skipDocumentArrayCast) {
value = new MongooseDocumentArray(value, this.path, doc);
} else if (value && value.isMongooseDocumentArray) {
// We need to create a new array, otherwise change tracking will
// update the old doc (gh-4449)
value = new MongooseDocumentArray(value, this.path, doc);
}

if (options.arrayPath != null) {
value[arrayPathSymbol] = options.arrayPath;
}

const len = value.length;

for (let i = 0; i < len; ++i) {
Expand Down

0 comments on commit 7911d92

Please sign in to comment.