Skip to content

Commit

Permalink
fix(model): allow excluding schema-level selected fields from project…
Browse files Browse the repository at this point in the history
…ion, including discriminator key

Fix #11546
  • Loading branch information
vkarpov15 committed Apr 20, 2022
1 parent 89a2384 commit eb62a79
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 25 deletions.
12 changes: 0 additions & 12 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2168,14 +2168,7 @@ Model.find = function find(conditions, projection, options, callback) {

const mq = new this.Query({}, {}, this, this.$__collection);
mq.select(projection);

mq.setOptions(options);
if (this.schema.discriminatorMapping &&
this.schema.discriminatorMapping.isRoot &&
mq.selectedInclusively()) {
// Need to select discriminator key because original schema doesn't have it
mq.select(this.schema.options.discriminatorKey);
}

callback = this.$handleCallbackError(callback);

Expand Down Expand Up @@ -2281,11 +2274,6 @@ Model.findOne = function findOne(conditions, projection, options, callback) {
const mq = new this.Query({}, {}, this, this.$__collection);
mq.select(projection);
mq.setOptions(options);
if (this.schema.discriminatorMapping &&
this.schema.discriminatorMapping.isRoot &&
mq.selectedInclusively()) {
mq.select(this.schema.options.discriminatorKey);
}

callback = this.$handleCallbackError(callback);
return mq.findOne(conditions, callback);
Expand Down
10 changes: 9 additions & 1 deletion lib/queryhelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,15 @@ exports.applyPaths = function applyPaths(fields, schema) {
delete fields[plusPath];
}

if (typeof type.selected !== 'boolean') return;
if (typeof type.selected !== 'boolean') {
return;
}

// If set to 0, we're excluding a selected path. See gh-11546
if (exclude && type.selected && fields[path] != null && !fields[path]) {
delete fields[path];
return;
}

if (hasPlusPath) {
// forced inclusion
Expand Down
22 changes: 12 additions & 10 deletions test/model.discriminator.querying.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,19 @@ describe('model', function() {
});

describe('findOne', function() {
it('when selecting `select: false` field (gh-4629)', function(done) {
it('when selecting `select: false` field (gh-4629)', async function() {
const s = new SecretEvent({ name: 'test', secret: 'test2' });
s.save(function(error) {
assert.ifError(error);
SecretEvent.findById(s._id, '+secret', function(error, doc) {
assert.ifError(error);
assert.equal(doc.name, 'test');
assert.equal(doc.secret, 'test2');
done();
});
});
await s.save();

let doc = await SecretEvent.findById(s._id, '+secret');
assert.equal(doc.__t, 'Secret');
assert.equal(doc.name, 'test');
assert.equal(doc.secret, 'test2');

doc = await BaseEvent.findOne({ _id: s._id }, 'name -__t');
assert.strictEqual(doc.__t, undefined);
assert.equal(doc.name, 'test');
assert.strictEqual(doc.secret, undefined);
});

it('select: false in base schema (gh-5448)', async function() {
Expand Down
2 changes: 0 additions & 2 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7238,7 +7238,6 @@ describe('model: populate:', function() {
});

it('document, and subdocuments are not lean by default', async function() {

const user = await db.model('User').findOne().populate({
path: 'roomId',
populate: {
Expand All @@ -7252,7 +7251,6 @@ describe('model: populate:', function() {
});

it('.lean() makes query result, and all populated fields lean', async function() {

const user = await db.model('User').findOne().
populate({
path: 'roomId',
Expand Down

0 comments on commit eb62a79

Please sign in to comment.