Skip to content

Commit

Permalink
Merge pull request #12569 from Automattic/vkarpov15/gh-12480
Browse files Browse the repository at this point in the history
fix(document): allow creating document with document array and top-level key named `schema`
  • Loading branch information
vkarpov15 committed Oct 20, 2022
2 parents 03f19f5 + 3141cb6 commit 9133393
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/types/DocumentArray/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function MongooseDocumentArray(values, path, doc) {
// to make more proof against unusual node environments
if (doc && doc instanceof Document) {
internals[arrayParentSymbol] = doc;
internals[arraySchemaSymbol] = doc.schema.path(path);
internals[arraySchemaSymbol] = doc.$__schema.path(path);

// `schema.path()` doesn't drill into nested arrays properly yet, see
// gh-6398, gh-6602. This is a workaround because nested arrays are
Expand Down
20 changes: 20 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11928,6 +11928,26 @@ describe('document', function() {
assert.ok(rawDoc);
assert.deepStrictEqual(rawDoc.tags, ['mongodb']);
});

it('can create document with document array and top-level key named `schema` (gh-12480)', async function() {
const AuthorSchema = new Schema({
fullName: { type: 'String', required: true }
});

const BookSchema = new Schema({
schema: { type: 'String', required: true },
title: { type: 'String', required: true },
authors: [AuthorSchema]
}, { supressReservedKeysWarning: true });

const Book = db.model('Book', BookSchema);

await Book.create({
schema: 'design',
authors: [{ fullName: 'Sourabh Bagrecha' }],
title: 'The power of JavaScript'
});
});
});

describe('Check if instance function that is supplied in schema option is availabe', function() {
Expand Down

0 comments on commit 9133393

Please sign in to comment.