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: prevent overwriting user defined writeConcern #13612

Merged
merged 5 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/helpers/schema/applyWriteConcern.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
const get = require('../get');

module.exports = function applyWriteConcern(schema, options) {
const writeConcern = get(schema, 'options.writeConcern', {});
const writeConcern = options.writeConcern ? options : get(schema, 'options.writeConcern', {});
if (Object.keys(writeConcern).length != 0) {
options.writeConcern = {};
if (!options.writeConcern) {
options.writeConcern = {};
}
if (!('w' in options) && writeConcern.w != null) {
options.writeConcern.w = writeConcern.w;
}
Expand Down
1 change: 0 additions & 1 deletion lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -1954,7 +1954,6 @@ Query.prototype._optionsForExec = function(model) {
if (!model) {
return options;
}

// Apply schema-level `writeConcern` option
applyWriteConcern(model.schema, options);

Expand Down
33 changes: 33 additions & 0 deletions test/helpers/applyWriteConcern.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

const assert = require('assert');
const applyWriteConcern = require('../../lib/helpers/schema/applyWriteConcern');
const start = require('../common');
const mongoose = start.mongoose;

describe('applyWriteConcern', function() {
let db;
before(function() {
db = start();
});
after(async function() {
await db.close();
});
it('should not overwrite user specified writeConcern options gh-13592', async function() {
const options = { writeConcern: { w: 'majority' } };
const testSchema = new mongoose.Schema({ name: String }, { writeConcern: { w: 0 } });
const Test = db.model('Test', testSchema);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't create a model in this test. Just use applyWriteConcern(), no need to use deleteMany().

await Test.create({ name: 'Test Testerson' });
applyWriteConcern(testSchema, options);
assert.deepStrictEqual({ writeConcern: { w: 'majority' } }, options);
await Test.deleteMany({}, options);
assert.deepStrictEqual({ writeConcern: { w: 'majority' } }, options);
await Test.deleteMany({});
/**
* Because no options were passed in, it is using the schema level writeConcern options.
* However, because we are ensuring that user specified options are not being overwritten,
* this is the only reasonable way to test this case as our options object should not match the schema options.
*/
assert.deepStrictEqual({ writeConcern: { w: 'majority' } }, options);
});
});