Skip to content

Commit

Permalink
Merge pull request #13338 from Automattic/vkarpov15/gh-13264
Browse files Browse the repository at this point in the history
fix(query): set ObjectParameterError if calling `findOneAndX()` with filter as non-object
  • Loading branch information
vkarpov15 committed Apr 27, 2023
2 parents c6398f3 + a50aa54 commit 2bb0fc2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/error/objectParameter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ObjectParameterError extends MongooseError {
*/
constructor(value, paramName, fnName) {
super('Parameter "' + paramName + '" to ' + fnName +
'() must be an object, got ' + value.toString());
'() must be an object, got "' + value.toString() + '" (type ' + typeof value + ')');
}
}

Expand Down
46 changes: 29 additions & 17 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3411,7 +3411,7 @@ function prepareDiscriminatorCriteria(query) {
* @api public
*/

Query.prototype.findOneAndUpdate = function(criteria, doc, options, callback) {
Query.prototype.findOneAndUpdate = function(filter, doc, options, callback) {
this.op = 'findOneAndUpdate';
this._validateOp();
this._validate();
Expand All @@ -3426,23 +3426,27 @@ Query.prototype.findOneAndUpdate = function(criteria, doc, options, callback) {
case 2:
if (typeof doc === 'function') {
callback = doc;
doc = criteria;
criteria = undefined;
doc = filter;
filter = undefined;
}
options = undefined;
break;
case 1:
if (typeof criteria === 'function') {
callback = criteria;
criteria = options = doc = undefined;
if (typeof filter === 'function') {
callback = filter;
filter = options = doc = undefined;
} else {
doc = criteria;
criteria = options = undefined;
doc = filter;
filter = options = undefined;
}
}

if (mquery.canMerge(criteria)) {
this.merge(criteria);
if (mquery.canMerge(filter)) {
this.merge(filter);
} else if (filter != null) {
this.error(
new ObjectParameterError(filter, 'filter', 'findOneAndUpdate')
);
}

// apply doc
Expand Down Expand Up @@ -3620,7 +3624,7 @@ Query.prototype.findOneAndRemove = function(conditions, options, callback) {
*
* @method findOneAndDelete
* @memberOf Query
* @param {Object} [conditions]
* @param {Object} [filter]
* @param {Object} [options]
* @param {Boolean} [options.rawResult] if true, returns the [raw result from the MongoDB driver](https://mongodb.github.io/node-mongodb-native/4.9/interfaces/ModifyResult.html)
* @param {ClientSession} [options.session=null] The session associated with this query. See [transactions docs](/docs/transactions.html).
Expand All @@ -3631,7 +3635,7 @@ Query.prototype.findOneAndRemove = function(conditions, options, callback) {
* @api public
*/

Query.prototype.findOneAndDelete = function(conditions, options, callback) {
Query.prototype.findOneAndDelete = function(filter, options, callback) {
this.op = 'findOneAndDelete';
this._validateOp();
this._validate();
Expand All @@ -3644,16 +3648,20 @@ Query.prototype.findOneAndDelete = function(conditions, options, callback) {
}
break;
case 1:
if (typeof conditions === 'function') {
callback = conditions;
conditions = undefined;
if (typeof filter === 'function') {
callback = filter;
filter = undefined;
options = undefined;
}
break;
}

if (mquery.canMerge(conditions)) {
this.merge(conditions);
if (mquery.canMerge(filter)) {
this.merge(filter);
} else if (filter != null) {
this.error(
new ObjectParameterError(filter, 'filter', 'findOneAndDelete')
);
}

options && this.setOptions(options);
Expand Down Expand Up @@ -3789,6 +3797,10 @@ Query.prototype.findOneAndReplace = function(filter, replacement, options, callb

if (mquery.canMerge(filter)) {
this.merge(filter);
} else if (filter != null) {
this.error(
new ObjectParameterError(filter, 'filter', 'findOneAndReplace')
);
}

if (replacement != null) {
Expand Down
10 changes: 10 additions & 0 deletions test/model.findOneAndUpdate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2403,4 +2403,14 @@ describe('model: findOneAndUpdate:', function() {

assert.ifError(err);
});

it('throws error if filter is not an object (gh-13264)', async function() {
const schema = new Schema({ name: String });
const Model = db.model('Test', schema);

const err = await Model.findOneAndUpdate('foobar', { name: 'foo' }).then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'ObjectParameterError');
});

});

0 comments on commit 2bb0fc2

Please sign in to comment.