From 8027af448d7a588c4877f2cff0c9e041aabb848f Mon Sep 17 00:00:00 2001 From: Carlos Avilan Date: Fri, 26 Nov 2021 19:02:51 +0100 Subject: [PATCH] fix: don't delete dbName if authSource is provided --- src/connection_string.ts | 5 ----- test/unit/connection_string.test.js | 11 +++++++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/connection_string.ts b/src/connection_string.ts index ed897000dc..4fc5d817e0 100644 --- a/src/connection_string.ts +++ b/src/connection_string.ts @@ -291,11 +291,6 @@ export function parseOptions( } } - if (urlOptions.has('authSource')) { - // If authSource is an explicit key in the urlOptions we need to remove the dbName - urlOptions.delete('dbName'); - } - const objectOptions = new CaseInsensitiveMap( Object.entries(options).filter(([, v]) => v != null) ); diff --git a/test/unit/connection_string.test.js b/test/unit/connection_string.test.js index e17276e9c2..f96089a409 100644 --- a/test/unit/connection_string.test.js +++ b/test/unit/connection_string.test.js @@ -99,6 +99,17 @@ describe('Connection String', function () { expect(options.credentials.source).to.equal('0001'); }); + it('should not remove dbName from the options if authSource is provided', function () { + const dbName = 'my-db-name'; + const authSource = 'admin'; + const options = parseOptions( + `mongodb://myName:myPassword@localhost:27017/${dbName}?authSource=${authSource}` + ); + + expect(options).has.property('dbName', dbName); + expect(options.credentials).to.have.property('source', authSource); + }); + it('should parse a replicaSet with a leading number', function () { const options = parseOptions('mongodb://localhost/?replicaSet=123abc'); expect(options).to.have.property('replicaSet');