Skip to content

Commit

Permalink
fix(schema): make ArraySubdocuments apply _id defaults on init
Browse files Browse the repository at this point in the history
Fix #12264
  • Loading branch information
vkarpov15 committed Aug 23, 2022
1 parent 8c0800a commit 4a946ff
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/schema/documentarray.js
Expand Up @@ -392,7 +392,7 @@ DocumentArrayPath.prototype.getDefault = function(scope, init, options) {
};

const _toObjectOptions = Object.freeze({ transform: false, virtuals: false });
const initDocumentOptions = Object.freeze({ skipId: true, willInit: true });
const initDocumentOptions = Object.freeze({ skipId: false, willInit: true });

/**
* Casts contents
Expand Down
14 changes: 14 additions & 0 deletions test/types.documentarray.test.js
Expand Up @@ -748,4 +748,18 @@ describe('types.documentarray', function() {
doc.arr.push(subdoc);
await doc.validate();
});

it('applies _id default (gh-12264)', function() {
mongoose.deleteModel(/Test/);
const nestedArraySchema = Schema({
subDocArray: [{ name: String }]
});

const Model = db.model('Test', nestedArraySchema);
const doc = new Model().init({
subDocArray: [{ name: 'foo' }]
});

assert.ok(doc.subDocArray[0]._id instanceof mongoose.Types.ObjectId);
});
});

0 comments on commit 4a946ff

Please sign in to comment.