Skip to content
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
35 changes: 27 additions & 8 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -4304,7 +4304,7 @@
this._mongooseOptions,
this
);

Check failure on line 4307 in lib/query.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
if (_getOption(this, 'runValidators', false)) {
await this.validate(this[queryUpdateSymbol], options, false);
}
Expand All @@ -4315,7 +4315,7 @@

return this.mongooseCollection.updateOne(this._conditions, this[queryUpdateSymbol], options);
};

Check failure on line 4318 in lib/query.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
/**
* Execute a replaceOne query
*
Expand All @@ -4327,7 +4327,7 @@
*/
Query.prototype._replaceOne = async function _replaceOne() {
this._applyTranslateAliases();
this._castConditions();

Check failure on line 4330 in lib/query.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Trailing spaces not allowed
checkRequireFilter(this._conditions, this.options);
_castArrayFilters(this);
if (this.error() != null) {
Expand All @@ -4335,17 +4335,36 @@
}

const options = this._optionsForExec(this.model);
this[queryUpdateSymbol] = new this.model(this[queryUpdateSymbol], null, { skipId: true });
removeUnusedArrayFilters(this[queryUpdateSymbol], options);

if (_getOption(this, 'runValidators', false)) {
await this.validate(this[queryUpdateSymbol], options, true);
const modelOpts = { skipId: true };
if ('strict' in this._mongooseOptions) {
modelOpts.strict = this._mongooseOptions.strict;
}

if (typeof this[queryUpdateSymbol].toBSON === 'function') {
this[queryUpdateSymbol] = this[queryUpdateSymbol].toBSON();
}
const runValidators = _getOption(this, 'runValidators', false);

try {
this[queryUpdateSymbol] = new this.model(this[queryUpdateSymbol], null, modelOpts);

removeUnusedArrayFilters(this[queryUpdateSymbol], options);

if (runValidators) {
await this.validate(this[queryUpdateSymbol], options, true);
} else if (this[queryUpdateSymbol].$__.validationError) {
throw this[queryUpdateSymbol].$__.validationError;
}

if (typeof this[queryUpdateSymbol].toBSON === 'function') {
this[queryUpdateSymbol] = this[queryUpdateSymbol].toBSON();
}
} catch (err) {
if (err instanceof ValidationError) {
throw err;
}
const validationError = new ValidationError();
validationError.errors[err.path] = err;
throw validationError;
}

return this.mongooseCollection.replaceOne(this._conditions, this[queryUpdateSymbol], options);
};

Expand Down
48 changes: 48 additions & 0 deletions test/model.updateOne.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,54 @@ describe('model: updateOne:', function() {

});

it('replaceOne throws cast errors instead of removing the path (gh-16628)', async function() {
const schema = mongoose.Schema({ name: String, age: Number }, {
versionKey: false
});
const Model = db.model('Test', schema);

const m = await Model.create({ name: 'abc', age: 1 });
const err = await Model.replaceOne({ _id: m._id }, { name: 'test', age: 'not a number' }).
then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'ValidationError');
assert.equal(err.errors['age'].name, 'CastError');

const doc = await Model.findById(m._id).lean();
assert.deepStrictEqual(doc, { _id: m._id, name: 'abc', age: 1 });
});

it('replaceOne respects query-level strict option (gh-16628)', async function() {
const schema = mongoose.Schema({ name: String }, { versionKey: false });
const Model = db.model('Test', schema);

const m = await Model.create({ name: 'abc' });
const err = await Model.replaceOne({ _id: m._id }, { name: 'test', notInSchema: 'foo' }, { strict: 'throw' }).
then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'ValidationError');
assert.equal(err.errors['notInSchema'].name, 'StrictModeError');

await Model.replaceOne({ _id: m._id }, { name: 'test', notInSchema: 'foo' }, { strict: false });
const doc = await Model.findById(m._id).lean();
assert.deepStrictEqual(doc, { _id: m._id, name: 'test', notInSchema: 'foo' });
});

it('replaceOne wraps schema-level strict errors in a ValidationError (gh-16628)', async function() {
const schema = mongoose.Schema({ name: String }, { versionKey: false, strict: 'throw' });
const Model = db.model('Test', schema);

const m = await Model.create({ name: 'abc' });
const err = await Model.replaceOne({ _id: m._id }, { name: 'test', notInSchema: 'foo' }).
then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'ValidationError');
assert.equal(err.errors['notInSchema'].name, 'StrictModeError');

const doc = await Model.findById(m._id).lean();
assert.deepStrictEqual(doc, { _id: m._id, name: 'abc' });
});

it('mixed nested type casting (gh-3337)', async function() {
const Schema = mongoose.Schema({ attributes: {} }, { strict: true });
const Model = db.model('Test', Schema);
Expand Down
Loading