Skip to content

Commit

Permalink
fix(populate): handle multiple spaces when specifying paths to popula…
Browse files Browse the repository at this point in the history
…te using space-delimited paths

Fix #13951
  • Loading branch information
vkarpov15 committed Oct 16, 2023
1 parent 236a7a7 commit 6419a87
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
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

0 comments on commit 6419a87

Please sign in to comment.