From e73e822f09639855ceaddc6dd2ba700a05cc0b60 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Wed, 20 Mar 2024 15:44:16 -0400 Subject: [PATCH 1/6] fix(schema): avoid returning string 'nested' as schematype Fix #14435 Fix #14443 --- lib/schema.js | 12 +++- test/document.populate.test.js | 114 +++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+), 2 deletions(-) diff --git a/lib/schema.js b/lib/schema.js index 35e3de56b4d..0c60ff16ca9 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -1220,10 +1220,18 @@ function _getPath(schema, path, cleanPath) { return schema.paths[path]; } if (schema.subpaths.hasOwnProperty(cleanPath)) { - return schema.subpaths[cleanPath]; + const subpath = schema.subpaths[cleanPath]; + if (subpath === 'nested') { + return undefined; + } + return subpath; } if (schema.singleNestedPaths.hasOwnProperty(cleanPath) && typeof schema.singleNestedPaths[cleanPath] === 'object') { - return schema.singleNestedPaths[cleanPath]; + const singleNestedPath = schema.singleNestedPaths[cleanPath]; + if (singleNestedPath === 'nested') { + return undefined; + } + return singleNestedPath; } return null; diff --git a/test/document.populate.test.js b/test/document.populate.test.js index efb8848cd98..3c0f3262b7f 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -934,4 +934,118 @@ describe('document.populate', function() { assert.ok(foundBook.populated('authorId')); assert.ok(foundBook.authorId.populated('websiteId')); }); + + it('works when populating a nested document inside an array parent (gh-14435)', async function() { + const CodeSchema = new Schema({ + code: String + }); + + const UserSchema = new Schema({ + username: String, + extras: [ + new Schema({ + config: new Schema({ + paymentConfiguration: { + paymentMethods: [ + { + type: Schema.Types.ObjectId, + ref: 'Code' + } + ] + } + }) + }) + ] + }); + + const Code = db.model('Code', CodeSchema); + const CodeUser = db.model('CodeUser', UserSchema); + + const code = await new Code({ + code: 'test code' + }).save(); + + await new CodeUser({ + username: 'TestUser', + extras: [ + { + config: { + paymentConfiguration: { + paymentMethods: [code._id] + } + } + } + ] + }).save(); + + const codeUser = await CodeUser.findOne({ username: 'TestUser' }).populate( + 'extras.config.paymentConfiguration.paymentMethods' + ); + + assert.ok(codeUser.username); + assert.strictEqual(codeUser.username, 'TestUser'); + assert.ok(codeUser.extras); + assert.strictEqual(codeUser.extras.length, 1); + assert.ok(codeUser.extras[0]); + assert.ok(codeUser.extras[0].config); + assert.ok(codeUser.extras[0].config.paymentConfiguration); + assert.ok(codeUser.extras[0].config.paymentConfiguration.paymentMethods); + assert.strictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods.length, 1); + assert.deepStrictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods[0]._id, code._id); + assert.strictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods[0].code, 'test code'); + }); + + it('works when populating a nested document not inside an array parent (gh-14435)', async function() { + const CodeSchema = new Schema({ + code: String + }); + + const UserSchema = new Schema({ + username: String, + extras: new Schema({ + config: new Schema({ + paymentConfiguration: { + paymentMethods: [ + { + type: Schema.Types.ObjectId, + ref: 'Code' + } + ] + } + }) + }) + }); + + const Code = db.model('Code', CodeSchema); + const CodeUser = db.model('CodeUser', UserSchema); + + const code = await new Code({ + code: 'test code' + }).save(); + + await new CodeUser({ + username: 'TestUser', + extras: { + config: { + paymentConfiguration: { + paymentMethods: [code._id] + } + } + } + }).save(); + + const codeUser = await CodeUser.findOne({ username: 'TestUser' }).populate( + 'extras.config.paymentConfiguration.paymentMethods' + ); + + assert.ok(codeUser.username); + assert.strictEqual(codeUser.username, 'TestUser'); + assert.ok(codeUser.extras); + assert.ok(codeUser.extras.config); + assert.ok(codeUser.extras.config.paymentConfiguration); + assert.ok(codeUser.extras.config.paymentConfiguration.paymentMethods); + assert.strictEqual(codeUser.extras.config.paymentConfiguration.paymentMethods.length, 1); + assert.deepStrictEqual(codeUser.extras.config.paymentConfiguration.paymentMethods[0]._id, code._id); + assert.strictEqual(codeUser.extras.config.paymentConfiguration.paymentMethods[0].code, 'test code'); + }); }); From 2c79d414cec138b146b1e6ca431d721a3fee69b8 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 21 Mar 2024 17:39:45 -0400 Subject: [PATCH 2/6] Update test/document.populate.test.js Co-authored-by: hasezoey --- test/document.populate.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/document.populate.test.js b/test/document.populate.test.js index 3c0f3262b7f..8cd93d29c9e 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -1019,9 +1019,9 @@ describe('document.populate', function() { const Code = db.model('Code', CodeSchema); const CodeUser = db.model('CodeUser', UserSchema); - const code = await new Code({ + const code = await Code.create({ code: 'test code' - }).save(); + }); await new CodeUser({ username: 'TestUser', From 1a6228817b9ff34700a71a6851b17a71bf3ae420 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 21 Mar 2024 17:39:51 -0400 Subject: [PATCH 3/6] Update test/document.populate.test.js Co-authored-by: hasezoey --- test/document.populate.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/document.populate.test.js b/test/document.populate.test.js index 8cd93d29c9e..068a58b92f0 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -1023,7 +1023,7 @@ describe('document.populate', function() { code: 'test code' }); - await new CodeUser({ + await CodeUser.create({ username: 'TestUser', extras: { config: { @@ -1032,7 +1032,7 @@ describe('document.populate', function() { } } } - }).save(); + }); const codeUser = await CodeUser.findOne({ username: 'TestUser' }).populate( 'extras.config.paymentConfiguration.paymentMethods' From 8310207dc2bf0f4d4bfa610b08d5aeedbe9d3286 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 21 Mar 2024 17:39:57 -0400 Subject: [PATCH 4/6] Update test/document.populate.test.js Co-authored-by: hasezoey --- test/document.populate.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/document.populate.test.js b/test/document.populate.test.js index 068a58b92f0..22e0508511f 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -965,7 +965,7 @@ describe('document.populate', function() { code: 'test code' }).save(); - await new CodeUser({ + await CodeUser.create({ username: 'TestUser', extras: [ { @@ -976,7 +976,7 @@ describe('document.populate', function() { } } ] - }).save(); + }); const codeUser = await CodeUser.findOne({ username: 'TestUser' }).populate( 'extras.config.paymentConfiguration.paymentMethods' From 1ce7a72c962414614220aae1cae59c1ed72301ab Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 21 Mar 2024 17:40:02 -0400 Subject: [PATCH 5/6] Update test/document.populate.test.js Co-authored-by: hasezoey --- test/document.populate.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/document.populate.test.js b/test/document.populate.test.js index 22e0508511f..d1f25dad897 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -961,9 +961,9 @@ describe('document.populate', function() { const Code = db.model('Code', CodeSchema); const CodeUser = db.model('CodeUser', UserSchema); - const code = await new Code({ + const code = await Code.create({ code: 'test code' - }).save(); + }); await CodeUser.create({ username: 'TestUser', From e6970bb0f95f09a307cbfe1b4ef81adbab37beb4 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Thu, 21 Mar 2024 17:43:56 -0400 Subject: [PATCH 6/6] test: remove unnecessary test re: #14435 --- test/document.populate.test.js | 54 ---------------------------------- 1 file changed, 54 deletions(-) diff --git a/test/document.populate.test.js b/test/document.populate.test.js index d1f25dad897..37e8ce3f600 100644 --- a/test/document.populate.test.js +++ b/test/document.populate.test.js @@ -994,58 +994,4 @@ describe('document.populate', function() { assert.deepStrictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods[0]._id, code._id); assert.strictEqual(codeUser.extras[0].config.paymentConfiguration.paymentMethods[0].code, 'test code'); }); - - it('works when populating a nested document not inside an array parent (gh-14435)', async function() { - const CodeSchema = new Schema({ - code: String - }); - - const UserSchema = new Schema({ - username: String, - extras: new Schema({ - config: new Schema({ - paymentConfiguration: { - paymentMethods: [ - { - type: Schema.Types.ObjectId, - ref: 'Code' - } - ] - } - }) - }) - }); - - const Code = db.model('Code', CodeSchema); - const CodeUser = db.model('CodeUser', UserSchema); - - const code = await Code.create({ - code: 'test code' - }); - - await CodeUser.create({ - username: 'TestUser', - extras: { - config: { - paymentConfiguration: { - paymentMethods: [code._id] - } - } - } - }); - - const codeUser = await CodeUser.findOne({ username: 'TestUser' }).populate( - 'extras.config.paymentConfiguration.paymentMethods' - ); - - assert.ok(codeUser.username); - assert.strictEqual(codeUser.username, 'TestUser'); - assert.ok(codeUser.extras); - assert.ok(codeUser.extras.config); - assert.ok(codeUser.extras.config.paymentConfiguration); - assert.ok(codeUser.extras.config.paymentConfiguration.paymentMethods); - assert.strictEqual(codeUser.extras.config.paymentConfiguration.paymentMethods.length, 1); - assert.deepStrictEqual(codeUser.extras.config.paymentConfiguration.paymentMethods[0]._id, code._id); - assert.strictEqual(codeUser.extras.config.paymentConfiguration.paymentMethods[0].code, 'test code'); - }); });