Skip to content

Commit

Permalink
fix(executeOperation): don't mutate options passed to commands
Browse files Browse the repository at this point in the history
When the topology believes it has session support, options passed to
commands were getting mutated with the session added to them. It is our
expectation that the driver will never be mutating arguments passed to
it via the public facing API.

This commit creates a new object and copies into it, then replaces in
the args array.
  • Loading branch information
durran authored and mbroadst committed Feb 20, 2018
1 parent 46e14d1 commit 934a43a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ const executeOperation = (topology, operation, args, options) => {
opOptions = args[args.length - 2];
if (opOptions == null || opOptions.session == null) {
session = topology.startSession();
Object.assign(args[args.length - 2], { session: session });
const optionsIndex = args.length - 2;
args[optionsIndex] = Object.assign({}, args[optionsIndex], { session: session });
} else if (opOptions.session && opOptions.session.hasEnded) {
throw new MongoError('Use of expired sessions is not permitted');
}
Expand Down
25 changes: 25 additions & 0 deletions test/unit/sessions/collection_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,30 @@ describe('Sessions', function() {
});
}
});

it('does not mutate command options', {
metadata: { requires: { topology: 'single' } },

test: function() {
const options = Object.freeze({});
test.server.setMessageHandler(request => {
const doc = request.document;
if (doc.ismaster) {
request.reply(mock.DEFAULT_ISMASTER_36);
} else if (doc.count) {
request.reply({ ok: 1 });
}
});

return MongoClient.connect(`mongodb://${test.server.uri()}/test`).then(client => {
const coll = client.db('foo').collection('bar');

return coll.count({}, options).then(() => {
expect(options).to.deep.equal({});
return client.close();
});
});
}
});
});
});

0 comments on commit 934a43a

Please sign in to comment.