Skip to content

Commit d7b2bde

Browse files
authored
Merge pull request #13984 from Automattic/vkarpov15/gh-13951
fix(populate): handle multiple spaces when specifying paths to populate using space-delimited paths
2 parents d7a3052 + 6419a87 commit d7b2bde

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/utils.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ exports.isMongooseDocumentArray = isMongooseDocumentArray.isMongooseDocumentArra
3131
exports.registerMongooseArray = isMongooseArray.registerMongooseArray;
3232
exports.registerMongooseDocumentArray = isMongooseDocumentArray.registerMongooseDocumentArray;
3333

34+
const oneSpaceRE = /\s/;
35+
const manySpaceRE = /\s+/;
36+
3437
/**
3538
* Produces a collection name from model `name`. By default, just returns
3639
* the model name
@@ -572,8 +575,8 @@ exports.populate = function populate(path, select, model, match, options, subPop
572575
function makeSingles(arr) {
573576
const ret = [];
574577
arr.forEach(function(obj) {
575-
if (/[\s]/.test(obj.path)) {
576-
const paths = obj.path.split(' ');
578+
if (oneSpaceRE.test(obj.path)) {
579+
const paths = obj.path.split(manySpaceRE);
577580
paths.forEach(function(p) {
578581
const copy = Object.assign({}, obj);
579582
copy.path = p;
@@ -592,9 +595,9 @@ function _populateObj(obj) {
592595
if (Array.isArray(obj.populate)) {
593596
const ret = [];
594597
obj.populate.forEach(function(obj) {
595-
if (/[\s]/.test(obj.path)) {
598+
if (oneSpaceRE.test(obj.path)) {
596599
const copy = Object.assign({}, obj);
597-
const paths = copy.path.split(' ');
600+
const paths = copy.path.split(manySpaceRE);
598601
paths.forEach(function(p) {
599602
copy.path = p;
600603
ret.push(exports.populate(copy)[0]);
@@ -609,7 +612,7 @@ function _populateObj(obj) {
609612
}
610613

611614
const ret = [];
612-
const paths = obj.path.split(' ');
615+
const paths = oneSpaceRE.test(obj.path) ? obj.path.split(manySpaceRE) : [obj.path];
613616
if (obj.options != null) {
614617
obj.options = clone(obj.options);
615618
}

test/model.populate.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2819,7 +2819,28 @@ describe('model: populate:', function() {
28192819

28202820
assert.equal(blogposts[0].user.name, 'Fan 1');
28212821
assert.equal(blogposts[0].title, 'Test 1');
2822+
});
2823+
2824+
it('handles multiple spaces in between paths to populate (gh-13951)', async function() {
2825+
const BlogPost = db.model('BlogPost', new Schema({
2826+
title: String,
2827+
user: { type: ObjectId, ref: 'User' },
2828+
fans: [{ type: ObjectId, ref: 'User' }]
2829+
}));
2830+
const User = db.model('User', new Schema({ name: String }));
2831+
2832+
const fans = await User.create([{ name: 'Fan 1' }]);
2833+
const posts = [
2834+
{ title: 'Test 1', user: fans[0]._id, fans: [fans[0]._id] }
2835+
];
2836+
await BlogPost.create(posts);
2837+
const blogPost = await BlogPost.
2838+
findOne({ title: 'Test 1' }).
2839+
populate('user \t fans');
28222840

2841+
assert.equal(blogPost.user.name, 'Fan 1');
2842+
assert.equal(blogPost.fans[0].name, 'Fan 1');
2843+
assert.equal(blogPost.title, 'Test 1');
28232844
});
28242845

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

0 commit comments

Comments
 (0)