diff --git a/lib/helpers/processConnectionOptions.js b/lib/helpers/processConnectionOptions.js index a9d862b1030..1dbb767ebee 100644 --- a/lib/helpers/processConnectionOptions.js +++ b/lib/helpers/processConnectionOptions.js @@ -9,11 +9,12 @@ function processConnectionOptions(uri, options) { ? opts.readPreference : getUriReadPreference(uri); + const clonedOpts = clone(opts); const resolvedOpts = (readPreference && readPreference !== 'primary' && readPreference !== 'primaryPreferred') - ? resolveOptsConflicts(readPreference, opts) - : opts; + ? resolveOptsConflicts(readPreference, clonedOpts) + : clonedOpts; - return clone(resolvedOpts); + return resolvedOpts; } function resolveOptsConflicts(pref, opts) { diff --git a/test/connection.test.js b/test/connection.test.js index dcf4cf621c7..49f715a8f4a 100644 --- a/test/connection.test.js +++ b/test/connection.test.js @@ -1537,4 +1537,13 @@ describe('connections:', function() { }); assert.deepEqual(m.connections.length, 0); }); + + describe('processConnectionOptions', function() { + it('should not throw an error when attempting to mutate unmutable options object gh-13335', async function() { + const m = new mongoose.Mongoose(); + const opts = Object.preventExtensions({}); + const conn = await m.connect('mongodb://localhost:27017/db?retryWrites=true&w=majority&readPreference=secondaryPreferred', opts); + assert.ok(conn); + }); + }); });