Skip to content

Commit

Permalink
Merge pull request #7207 from lineus/fix-7098
Browse files Browse the repository at this point in the history
fixes #7098 don't skip casting the update for array of array values
  • Loading branch information
vkarpov15 committed Nov 2, 2018
2 parents e9d538e + 64c6d15 commit c1606b6
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/helpers/query/castUpdate.js
Expand Up @@ -411,7 +411,7 @@ function castUpdateVal(schema, val, op, $conditional, context, path) {
return schema.castForQueryWrapper({
val: val,
context: context,
$skipQueryCastForUpdate: val != null && schema.$isMongooseArray
$skipQueryCastForUpdate: val != null && schema.$isMongooseArray && schema.$parentSchema
});
}

Expand Down
42 changes: 42 additions & 0 deletions test/model.update.test.js
Expand Up @@ -2536,6 +2536,48 @@ describe('model: update:', function() {
catch(done);
});

it('doesn\'t skip casting the query on nested arrays (gh-7098)', function() {
const nestedSchema = new Schema({
xyz: [[Number]]
});
const schema = new Schema({
xyz: [[{ type: Number }]],
nested: nestedSchema
});

const Model = db.model('gh-7098', schema);

const test = new Model({
xyz: [
[0, 1],
[2, 3],
[4, 5]
],
nested: {
xyz: [
[0, 1],
[2, 3],
[4, 5]
],
}
});

const cond = { _id: test._id };
const update = { $set: { 'xyz.1.0': '200', 'nested.xyz.1.0': '200' } };
const opts = { new: true };

return co(function*() {
let inserted = yield test.save();
inserted = inserted.toObject();
assert.deepStrictEqual(inserted.xyz, [[0, 1], [2, 3], [4, 5]]);
assert.deepStrictEqual(inserted.nested.xyz, [[0, 1], [2, 3], [4, 5]]);
let updated = yield Model.findOneAndUpdate(cond, update, opts);
updated = updated.toObject();
assert.deepStrictEqual(updated.xyz, [[0, 1], [200, 3], [4, 5]]);
assert.deepStrictEqual(updated.nested.xyz, [[0, 1], [200, 3], [4, 5]]);
});
});

it('defaults with overwrite and no update validators (gh-5384)', function(done) {
const testSchema = new mongoose.Schema({
name: String,
Expand Down

0 comments on commit c1606b6

Please sign in to comment.