Skip to content
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: added fix to populate a nested document field inside an array parent when using path string #14443

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/document.js
Expand Up @@ -1929,7 +1929,7 @@ Document.prototype.get = function(path, type, options) {
obj = adhoc.cast(obj);
}

if (schema != null && options.getters !== false) {
if (schema != null && schema !== 'nested' && options.getters !== false) {
obj = schema.applyGetters(obj, this);
} else if (this.$__schema.nested[path] && options.virtuals) {
// Might need to apply virtuals if this is a nested path
Expand Down
114 changes: 114 additions & 0 deletions test/document.populate.test.js
Expand Up @@ -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');
});
});