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(populate): handle multiple spaces when specifying paths to populate using space-delimited paths #13984

Merged
merged 1 commit into from
Oct 17, 2023
Merged
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
13 changes: 8 additions & 5 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ exports.isMongooseDocumentArray = isMongooseDocumentArray.isMongooseDocumentArra
exports.registerMongooseArray = isMongooseArray.registerMongooseArray;
exports.registerMongooseDocumentArray = isMongooseDocumentArray.registerMongooseDocumentArray;

const oneSpaceRE = /\s/;
const manySpaceRE = /\s+/;

/**
* Produces a collection name from model `name`. By default, just returns
* the model name
Expand Down Expand Up @@ -572,8 +575,8 @@ exports.populate = function populate(path, select, model, match, options, subPop
function makeSingles(arr) {
const ret = [];
arr.forEach(function(obj) {
if (/[\s]/.test(obj.path)) {
const paths = obj.path.split(' ');
if (oneSpaceRE.test(obj.path)) {
const paths = obj.path.split(manySpaceRE);
paths.forEach(function(p) {
const copy = Object.assign({}, obj);
copy.path = p;
Expand All @@ -592,9 +595,9 @@ function _populateObj(obj) {
if (Array.isArray(obj.populate)) {
const ret = [];
obj.populate.forEach(function(obj) {
if (/[\s]/.test(obj.path)) {
if (oneSpaceRE.test(obj.path)) {
const copy = Object.assign({}, obj);
const paths = copy.path.split(' ');
const paths = copy.path.split(manySpaceRE);
paths.forEach(function(p) {
copy.path = p;
ret.push(exports.populate(copy)[0]);
Expand All @@ -609,7 +612,7 @@ function _populateObj(obj) {
}

const ret = [];
const paths = obj.path.split(' ');
const paths = oneSpaceRE.test(obj.path) ? obj.path.split(manySpaceRE) : [obj.path];
if (obj.options != null) {
obj.options = clone(obj.options);
}
Expand Down
21 changes: 21 additions & 0 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2819,7 +2819,28 @@ describe('model: populate:', function() {

assert.equal(blogposts[0].user.name, 'Fan 1');
assert.equal(blogposts[0].title, 'Test 1');
});

it('handles multiple spaces in between paths to populate (gh-13951)', async function() {
const BlogPost = db.model('BlogPost', new Schema({
title: String,
user: { type: ObjectId, ref: 'User' },
fans: [{ type: ObjectId, ref: 'User' }]
}));
const User = db.model('User', new Schema({ name: String }));

const fans = await User.create([{ name: 'Fan 1' }]);
const posts = [
{ title: 'Test 1', user: fans[0]._id, fans: [fans[0]._id] }
];
await BlogPost.create(posts);
const blogPost = await BlogPost.
findOne({ title: 'Test 1' }).
populate('user \t fans');

assert.equal(blogPost.user.name, 'Fan 1');
assert.equal(blogPost.fans[0].name, 'Fan 1');
assert.equal(blogPost.title, 'Test 1');
});

it('maps results back to correct document (gh-1444)', async function() {
Expand Down