Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(wireprotocol): only send bypassDocumentValidation if true
Browse files Browse the repository at this point in the history
The bypassDocumentValidation key will only be set if it explicitly
receives a true, otherwise it remains undefined.

Fixes NODE-1492
  • Loading branch information
Sophie Saskin authored Jun 14, 2018
1 parent 6fec2ce commit a81678b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/wireprotocol/2_6_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var executeWrite = function(pool, bson, type, opsField, ns, ops, options, callba
}

// Do we have bypassDocumentValidation set, then enable it on the write command
if (typeof options.bypassDocumentValidation === 'boolean') {
if (options.bypassDocumentValidation === true) {
writeCommand.bypassDocumentValidation = options.bypassDocumentValidation;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/wireprotocol/3_2_support.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ var executeWrite = function(pool, bson, type, opsField, ns, ops, options, callba
}

// Do we have bypassDocumentValidation set, then enable it on the write command
if (typeof options.bypassDocumentValidation === 'boolean') {
if (options.bypassDocumentValidation === true) {
writeCommand.bypassDocumentValidation = options.bypassDocumentValidation;
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"mongodb-mock-server": "^1.0.0",
"mongodb-test-runner": "^1.1.18",
"prettier": "~1.12.0",
"sinon": "^6.0.0",
"snappy": "^6.0.1"
},
"peerOptionalDependencies": {
Expand Down
47 changes: 47 additions & 0 deletions test/tests/unit/wire_protocol_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict';

const chai = require('chai');
const expect = chai.expect;
const bson = require('bson');
const sinon = require('sinon');
const Pool = require('../../../lib/connection/pool.js');
const wireProtocol2_6 = require('../../../lib/wireprotocol/2_6_support.js');
const wireProtocol3_2 = require('../../../lib/wireprotocol/3_2_support.js');

describe('WireProtocol', function() {
it('2.6 should only set bypassDocumentValidation to true if explicitly set by user to true', function() {
testPoolWrite(true, new wireProtocol2_6(), true);
});

it('2.6 should not set bypassDocumentValidation to anything if not explicitly set by user to true', function() {
testPoolWrite(false, new wireProtocol2_6(), undefined);
});

it('3.2 should only set bypassDocumentValidation to true if explicitly set by user to true', function() {
testPoolWrite(true, new wireProtocol3_2(), true);
});

it('3.2 should not set bypassDocumentValidation to anything if not explicitly set by user to true', function() {
testPoolWrite(false, new wireProtocol3_2(), undefined);
});

function testPoolWrite(bypassDocumentValidation, wireProtocol, expected) {
const pool = sinon.createStubInstance(Pool);
const isMaster = {};
const ns = 'fake.namespace';
const ops = [{ a: 1 }, { b: 2 }];
const options = { bypassDocumentValidation: bypassDocumentValidation };

wireProtocol.insert(pool, isMaster, ns, bson, ops, options, () => {});

if (expected) {
expect(pool.write.lastCall.args[0])
.to.have.nested.property('query.bypassDocumentValidation')
.that.equals(expected);
} else {
expect(pool.write.lastCall.args[0]).to.not.have.nested.property(
'query.bypassDocumentValidation'
);
}
}
});

0 comments on commit a81678b

Please sign in to comment.