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(NODE-3194): Ignore undefined and null options in MongoClient constructor #2800

Merged
merged 4 commits into from
May 10, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/connection_string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,9 @@ export function parseOptions(
}
}

const objectOptions = new CaseInsensitiveMap(Object.entries(options));
const objectOptions = new CaseInsensitiveMap(
Object.entries(options).filter(([, v]) => (v ?? null) !== null)
);

const allOptions = new CaseInsensitiveMap();

Expand Down Expand Up @@ -369,8 +371,10 @@ export function parseOptions(
Array.from(Object.keys(OPTIONS)).map(s => s.toLowerCase())
);
if (unsupportedOptions.size !== 0) {
const optionWord = unsupportedOptions.size > 1 ? 'options' : 'option';
const isOrAre = unsupportedOptions.size > 1 ? 'are' : 'is';
throw new MongoParseError(
`options ${Array.from(unsupportedOptions).join(', ')} are not supported`
`${optionWord} ${Array.from(unsupportedOptions).join(', ')} ${isOrAre} not supported`
);
}

Expand Down
41 changes: 37 additions & 4 deletions test/unit/mongo_client_options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { WriteConcern } = require('../../src/write_concern');
const { ReadPreference } = require('../../src/read_preference');
const { Logger } = require('../../src/logger');
const { MongoCredentials } = require('../../src/cmap/auth/mongo_credentials');
const { MongoClient } = require('../../src');
const { MongoClient, MongoParseError } = require('../../src');

describe('MongoOptions', function () {
it('MongoClient should always freeze public options', function () {
Expand Down Expand Up @@ -138,9 +138,8 @@ describe('MongoOptions', function () {
zlibCompressionLevel: 2
};

it('All options', function () {
it('should parse all options from the options object', function () {
const options = parseOptions('mongodb://localhost:27017/', ALL_OPTIONS);

// Check consolidated options
expect(options).has.property('writeConcern');
expect(options.writeConcern).has.property('w', 2);
Expand Down Expand Up @@ -183,7 +182,7 @@ describe('MongoOptions', function () {
'zlibCompressionLevel=2'
].join('&');

it('All URI options', function () {
it('should parse all options from the URI string', function () {
const options = parseOptions(allURIOptions);
expect(options).has.property('zlibCompressionLevel', 2);

Expand All @@ -192,6 +191,40 @@ describe('MongoOptions', function () {
expect(options.writeConcern).has.property('wtimeout', 2);
});

it('should ignore undefined and null values in the options object', function () {
const options = parseOptions('mongodb://localhost:27017/', {
maxPoolSize: null,
servername: undefined,
randomopt: null,
otherrandomopt: undefined
});

// test valid option key with default value
expect(options).to.have.property('maxPoolSize', 100);

// test valid option key without default value
expect(options).not.to.have.property('servername');

// test invalid option keys that are null/undefined
expect(options).not.to.have.property('randomopt');
expect(options).not.to.have.property('otherrandomopt');
});

it('should throw an error on unrecognized keys in the options object if they are defined', function () {
expect(() =>
parseOptions('mongodb://localhost:27017/', {
randomopt: 'test'
})
).to.throw(MongoParseError, 'option randomopt is not supported');

expect(() =>
parseOptions('mongodb://localhost:27017/', {
randomopt: 'test',
randomopt2: 'test'
})
).to.throw(MongoParseError, 'options randomopt, randomopt2 are not supported');
});

it('srvHost saved to options for later resolution', function () {
const options = parseOptions('mongodb+srv://server.example.com/');
expect(options).has.property('srvHost', 'server.example.com');
Expand Down