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: don't modify passed options object #13376

Merged
merged 7 commits into from
May 4, 2023
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
1 change: 0 additions & 1 deletion lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,6 @@ async function _createMongoClient(conn, uri, options) {
options = processConnectionOptions(uri, options);

if (options) {
options = clone(options);

const autoIndex = options.config && options.config.autoIndex != null ?
options.config.autoIndex :
Expand Down
7 changes: 4 additions & 3 deletions lib/helpers/processConnectionOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions test/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1547,4 +1547,16 @@ describe('connections:', function() {
// currently cannot write test for continueOnError or errors in general.
});
});
describe('processConnectionOptions', function() {
IslandRhythms marked this conversation as resolved.
Show resolved Hide resolved
let m = null;
after(async() => {
await m.disconnect();
});
it('should not throw an error when attempting to mutate unmutable options object gh-13335', async function() {
m = new mongoose.Mongoose();
const opts = Object.preventExtensions({ readPreference: 'secondaryPreferred' });
const conn = await m.connect(start.uri, opts);
assert.ok(conn);
});
});
});