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): set ObjectParameterError if calling findOneAndX() with filter as non-object #13338

Merged
merged 2 commits into from
Apr 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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');
});

});