Skip to content

Commit 5ebe00a

Browse files
authored
Merge pull request Automattic#14894 from Automattic/vkarpov15/Automatticgh-14893
fix(projection): avoid setting projection to unknown exclusive/inclusive if elemMatch on a Date, ObjectId, etc.
2 parents 3cfd3f6 + bd4440a commit 5ebe00a

File tree

4 files changed

+32
-4
lines changed

4 files changed

+32
-4
lines changed

lib/helpers/projection/isExclusive.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const isDefiningProjection = require('./isDefiningProjection');
4+
const isPOJO = require('../isPOJO');
45

56
/*!
67
* ignore
@@ -22,10 +23,12 @@ module.exports = function isExclusive(projection) {
2223
// Explicitly avoid `$meta` and `$slice`
2324
const key = keys[ki];
2425
if (key !== '_id' && isDefiningProjection(projection[key])) {
25-
exclude = (projection[key] != null && typeof projection[key] === 'object') ?
26-
isExclusive(projection[key]) :
26+
exclude = isPOJO(projection[key]) ?
27+
(isExclusive(projection[key]) ?? exclude) :
2728
!projection[key];
28-
break;
29+
if (exclude != null) {
30+
break;
31+
}
2932
}
3033
}
3134
}

lib/helpers/projection/isInclusive.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22

33
const isDefiningProjection = require('./isDefiningProjection');
4+
const isPOJO = require('../isPOJO');
45

56
/*!
67
* ignore
@@ -26,7 +27,7 @@ module.exports = function isInclusive(projection) {
2627
// If field is truthy (1, true, etc.) and not an object, then this
2728
// projection must be inclusive. If object, assume its $meta, $slice, etc.
2829
if (isDefiningProjection(projection[prop]) && !!projection[prop]) {
29-
if (projection[prop] != null && typeof projection[prop] === 'object') {
30+
if (isPOJO(projection[prop])) {
3031
return isInclusive(projection[prop]);
3132
} else {
3233
return !!projection[prop];
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
require('../common'); // required for side-effect setup (so that the default driver is set-up)
6+
const isExclusive = require('../../lib/helpers/projection/isExclusive');
7+
8+
describe('isExclusive', function() {
9+
it('handles $elemMatch (gh-14893)', function() {
10+
assert.strictEqual(isExclusive({ field: { $elemMatch: { test: new Date('2024-06-01') } }, otherProp: 1 }), false);
11+
});
12+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
5+
require('../common'); // required for side-effect setup (so that the default driver is set-up)
6+
const isInclusive = require('../../lib/helpers/projection/isInclusive');
7+
8+
describe('isInclusive', function() {
9+
it('handles $elemMatch (gh-14893)', function() {
10+
assert.strictEqual(isInclusive({ field: { $elemMatch: { test: new Date('2024-06-01') } }, otherProp: 1 }), true);
11+
});
12+
});

0 commit comments

Comments
 (0)