Skip to content

fix: report default paths in VersionError message because they can can cause VersionError #15464

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

Merged
merged 1 commit into from
Jun 5, 2025
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
14 changes: 9 additions & 5 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,8 @@ Model.prototype.$__handleSave = function(options, callback) {
return;
}

// store the modified paths before the document is reset
this.$__.modifiedPaths = this.modifiedPaths();
// store the modified paths before the document is reset in case we need to generate version error.
this.$__.modifiedPaths = this.modifiedPaths().concat(Object.keys(this.$__.activePaths.getStatePaths('default')));
this.$__reset();

_setIsNew(this, false);
Expand Down Expand Up @@ -562,13 +562,13 @@ Model.prototype.$__save = function(options, callback) {
* ignore
*/

function generateVersionError(doc, modifiedPaths) {
function generateVersionError(doc, modifiedPaths, defaultPaths) {
const key = doc.$__schema.options.versionKey;
if (!key) {
return null;
}
const version = doc.$__getValue(key) || 0;
return new VersionError(doc, version, modifiedPaths);
return new VersionError(doc, version, modifiedPaths.concat(defaultPaths));
}

/**
Expand Down Expand Up @@ -626,7 +626,11 @@ Model.prototype.save = async function save(options) {
if (this.$__.timestamps != null) {
options.timestamps = this.$__.timestamps;
}
this.$__.$versionError = generateVersionError(this, this.modifiedPaths());
this.$__.$versionError = generateVersionError(
this,
this.modifiedPaths(),
Object.keys(this.$__.activePaths.getStatePaths('default'))
);

if (parallelSave) {
this.$__handleReject(parallelSave);
Expand Down
27 changes: 27 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14469,6 +14469,33 @@ describe('document', function() {
assert.strictEqual(strValidateCalls, 1);

});

it('triggers VersionError when using push() to an array', async function() {
const schema = new Schema({ texts: [{ text: String, _id: false }], test: String });
const Test = db.model('Test', schema);

const doc = new Test({ texts: [{ text: 'test' }], test: 'Test' });
await doc.save();
await Test.collection.updateOne({ _id: doc._id }, { $unset: { texts: 1 } });

// Get two instances of the same document to create a version conflict
const doc1 = await Test.findById(doc._id);
const doc2 = await Test.findById(doc._id);

// Modify and save the first instance
doc1.texts.push({ text: 'name1' });
await doc1.save();

// Try to modify and save the second instance
// This should cause a VersionError because `texts` gets a default value of `[]`.
doc2.test = null;

// Should throw VersionError because doc1 was saved with a new version number
const err = await doc2.save().then(() => null, err => err);
assert.ok(err);
assert.strictEqual(err.name, 'VersionError');
assert.ok(err.message.includes('texts'));
});
});

describe('Check if instance function that is supplied in schema option is available', function() {
Expand Down