Skip to content

Commit aa1bc46

Browse files
committed
Merge pull request #2486 from alabid/gh-2406
gh-2406 make copy of update parameters
2 parents e1e7f3e + 552556d commit aa1bc46

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/model.js

+6
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,12 @@ Model.create = function create (doc, fn) {
16291629

16301630
Model.update = function update (conditions, doc, options, callback) {
16311631
var mq = new Query({}, {}, this, this.collection);
1632+
// gh-2406
1633+
// make local deep copy of non-function parameters
1634+
conditions = utils.clone(conditions);
1635+
doc = utils.clone(doc);
1636+
options = typeof options === 'function' ? options : utils.clone(options);
1637+
16321638
return mq.update(conditions, doc, options, callback);
16331639
};
16341640

test/model.update.test.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ describe('model: update:', function(){
305305
});
306306
});
307307

308+
it('makes copy of conditions and update options', function(done) {
309+
var db = start()
310+
, BlogPost = db.model('BlogPostForUpdates', collection);
311+
312+
var conditions = { '_id': post._id.toString() };
313+
var update = {'$set':{'some_attrib':post._id.toString()}};
314+
BlogPost.update(conditions, update, function(err) {
315+
assert.ifError(err);
316+
assert.equal('string', typeof conditions._id);
317+
done();
318+
});
319+
});
320+
308321
it('handles weird casting (gh-479)', function(done){
309322
var db = start()
310323
, BlogPost = db.model('BlogPostForUpdates', collection)
@@ -357,7 +370,7 @@ describe('model: update:', function(){
357370
assert.equal(1, ret._doc.comments[0]._doc.newprop);
358371
assert.strictEqual(undefined, ret._doc.comments[1]._doc.newprop);
359372
assert.ok(ret.date instanceof Date);
360-
assert.equal(ret.date.toString(), update.$set.date.toString());
373+
assert.equal(ret.date.toString(), new Date(update.$set.date).toString());
361374

362375
last = ret;
363376
done();

0 commit comments

Comments
 (0)