Skip to content

chore: use Deno 2.1 in tests #15183

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

Merged
merged 11 commits into from
Jan 16, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v1.37.x
deno-version: v2.1.x
- run: deno --version
- run: npm install
- name: Run Deno tests
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
8.9.5 / 2025-01-13
==================
* fix: disallow nested $where in populate match
* fix(schema): handle bitwise operators on Int32 #15176 #15170

7.8.4 / 2025-01-13
===================
* fix: disallow nested $where in populate match

6.13.6 / 2025-01-13
===================
* fix: disallow nested $where in populate match

8.9.4 / 2025-01-09
==================
* fix(document): fix document not applying manual populate when using a function in schema.options.ref #15138 [IchirokuXVI](https://github.com/IchirokuXVI)
Expand Down
41 changes: 23 additions & 18 deletions lib/helpers/populate/getModelsMapForPopulate.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,7 @@ module.exports = function getModelsMapForPopulate(model, docs, options) {
if (hasMatchFunction) {
match = match.call(doc, doc);
}
if (Array.isArray(match)) {
for (const item of match) {
if (item != null && item.$where) {
throw new MongooseError('Cannot use $where filter with populate() match');
}
}
} else if (match != null && match.$where != null) {
throw new MongooseError('Cannot use $where filter with populate() match');
}
throwOn$where(match);
data.match = match;
data.hasMatchFunction = hasMatchFunction;
data.isRefPath = isRefPath;
Expand Down Expand Up @@ -463,15 +455,7 @@ function _virtualPopulate(model, docs, options, _virtualRes) {
data.match = match;
data.hasMatchFunction = hasMatchFunction;

if (Array.isArray(match)) {
for (const item of match) {
if (item != null && item.$where) {
throw new MongooseError('Cannot use $where filter with populate() match');
}
}
} else if (match != null && match.$where != null) {
throw new MongooseError('Cannot use $where filter with populate() match');
}
throwOn$where(match);

// Get local fields
const ret = _getLocalFieldValues(doc, localField, model, options, virtual);
Expand Down Expand Up @@ -759,3 +743,24 @@ function _findRefPathForDiscriminators(doc, modelSchema, data, options, normaliz

return modelNames;
}

/**
* Throw an error if there are any $where keys
*/

function throwOn$where(match) {
if (match == null) {
return;
}
if (typeof match !== 'object') {
return;
}
for (const key of Object.keys(match)) {
if (key === '$where') {
throw new MongooseError('Cannot use $where filter with populate() match');
}
if (match[key] != null && typeof match[key] === 'object') {
throwOn$where(match[key]);
}
}
}
2 changes: 1 addition & 1 deletion lib/schema/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ SchemaBigInt.prototype.castForQuery = function($conditional, val, context) {
return handler.call(this, val);
}

return this.applySetters(null, val, context);
return this.applySetters(val, context);
}

try {
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ SchemaBoolean.prototype.castForQuery = function($conditional, val, context) {
return handler.call(this, val);
}

return this.applySetters(null, val, context);
return this.applySetters(val, context);
}

try {
Expand Down
9 changes: 7 additions & 2 deletions lib/schema/int32.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const CastError = require('../error/cast');
const SchemaType = require('../schemaType');
const castInt32 = require('../cast/int32');
const handleBitwiseOperator = require('./operators/bitwise');

/**
* Int32 SchemaType constructor.
Expand Down Expand Up @@ -200,7 +201,11 @@ SchemaInt32.$conditionalHandlers = {
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
$lte: handleSingle,
$bitsAllClear: handleBitwiseOperator,
$bitsAnyClear: handleBitwiseOperator,
$bitsAllSet: handleBitwiseOperator,
$bitsAnySet: handleBitwiseOperator
};

/*!
Expand Down Expand Up @@ -228,7 +233,7 @@ SchemaInt32.prototype.castForQuery = function($conditional, val, context) {
return handler.call(this, val);
}

return this.applySetters(null, val, context);
return this.applySetters(val, context);
}

try {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "8.9.4",
"version": "8.9.5",
"author": "Guillermo Rauch <[email protected]>",
"keywords": [
"mongodb",
Expand All @@ -17,6 +17,7 @@
"orm",
"db"
],
"type": "commonjs",
"license": "MIT",
"dependencies": {
"bson": "^6.10.1",
Expand Down
12 changes: 12 additions & 0 deletions test/cast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ describe('cast: ', function() {
{ x: { $bitsAnyClear: Buffer.from([3]) } });
});

it('with int32 (gh-15170)', function() {
const schema = new Schema({ x: 'Int32' });
assert.deepEqual(cast(schema, { x: { $bitsAnySet: 3 } }),
{ x: { $bitsAnySet: 3 } });
});

it('throws when invalid', function() {
const schema = new Schema({ x: Number });
assert.throws(function() {
Expand Down Expand Up @@ -250,4 +256,10 @@ describe('cast: ', function() {
'state.fieldFoo': '44'
});
});

it('treats unknown operators as passthrough (gh-15170)', function() {
const schema = new Schema({ x: Boolean });
assert.deepEqual(cast(schema, { x: { $someConditional: 'true' } }),
{ x: { $someConditional: true } });
});
});
2 changes: 0 additions & 2 deletions test/deno.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ const fixtures = require('./mocha-fixtures.js')
await fixtures.mochaGlobalSetup();

const child_args = [
// args is required to be set manually, because there is currently no way to get all arguments from deno
'--allow-env', '--allow-read', '--allow-net', '--allow-run', '--allow-sys', '--allow-write',
...Deno.args,
resolve(fileURLToPath(import.meta.url), '../deno_mocha.js')
];
Expand Down
21 changes: 17 additions & 4 deletions test/model.populate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3670,21 +3670,34 @@ describe('model: populate:', function() {
const parent = await Parent.create({ name: 'Anakin', child: child._id });

await assert.rejects(
() => Parent.findOne().populate({ path: 'child', match: { $where: 'console.log("oops!");' } }),
() => Parent.findOne().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
/Cannot use \$where filter with populate\(\) match/
);
await assert.rejects(
() => Parent.find().populate({ path: 'child', match: { $where: 'console.log("oops!");' } }),
() => Parent.find().populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
/Cannot use \$where filter with populate\(\) match/
);
await assert.rejects(
() => parent.populate({ path: 'child', match: { $where: 'console.log("oops!");' } }),
() => parent.populate({ path: 'child', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
/Cannot use \$where filter with populate\(\) match/
);
await assert.rejects(
() => Child.find().populate({ path: 'parent', match: { $where: 'console.log("oops!");' } }),
() => Child.find().populate({ path: 'parent', match: () => ({ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }) }),
/Cannot use \$where filter with populate\(\) match/
);
await assert.rejects(
() => Child.find().populate({ path: 'parent', match: () => ({ $or: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }),
/Cannot use \$where filter with populate\(\) match/
);
await assert.rejects(
() => Child.find().populate({ path: 'parent', match: () => ({ $and: [{ $where: 'typeof console !== "undefined" ? doesNotExist("foo") : true;' }] }) }),
/Cannot use \$where filter with populate\(\) match/
);

class MyClass {}
MyClass.prototype.$where = 'typeof console !== "undefined" ? doesNotExist("foo") : true;';
// OK because sift only looks through own properties
await Child.find().populate({ path: 'parent', match: () => new MyClass() });
});

it('multiple source docs', async function() {
Expand Down
Loading