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(query+populate): add refPath to projection by default, unless explicitly excluded #13758

Merged
merged 1 commit into from
Aug 22, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions lib/helpers/query/selectPopulatedFields.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,25 @@ module.exports = function selectPopulatedFields(fields, userProvidedFields, popu
} else if (userProvidedFields[path] === 0) {
delete fields[path];
}

const refPath = populateOptions[path]?.refPath;
if (typeof refPath === 'string') {
if (!isPathInFields(userProvidedFields, refPath)) {
fields[refPath] = 1;
} else if (userProvidedFields[refPath] === 0) {
delete fields[refPath];
}
}
}
} else if (isExclusive(fields)) {
for (const path of paths) {
if (userProvidedFields[path] == null) {
delete fields[path];
}
const refPath = populateOptions[path]?.refPath;
if (typeof refPath === 'string' && userProvidedFields[refPath] == null) {
delete fields[refPath];
}
}
}
};
Expand Down
36 changes: 36 additions & 0 deletions test/helpers/query.selectPopulatedFields.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const assert = require('assert');
const selectPopulatedFields = require('../../lib/helpers/query/selectPopulatedFields');

describe('selectPopulatedFields', function() {
it('selects refPath', function() {
const fields = { name: 1 };
const userProvidedFields = { name: 1 };
const populateOptions = {
parent: {
refPath: 'parentModel'
}
};
selectPopulatedFields(fields, userProvidedFields, populateOptions);
assert.deepStrictEqual(fields, {
name: 1,
parent: 1,
parentModel: 1
});
});

it('adds refPath to projection if not deselected by user in exclusive projection', function() {
const fields = { name: 0, parentModel: 0 };
const userProvidedFields = { name: 0 };
const populateOptions = {
parent: {
refPath: 'parentModel'
}
};
selectPopulatedFields(fields, userProvidedFields, populateOptions);
assert.deepStrictEqual(fields, {
name: 0
});
});
});