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(document): save newly set defaults underneath single nested subdocuments #13002

Merged
merged 1 commit into from
Feb 6, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions lib/helpers/document/applyDefaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre

if (typeof def !== 'undefined') {
doc_[piece] = def;
doc.$__.activePaths.default(p);
applyChangeTracking(doc, p);
}
} else if (included) {
// selected field
Expand All @@ -91,7 +91,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre

if (typeof def !== 'undefined') {
doc_[piece] = def;
doc.$__.activePaths.default(p);
applyChangeTracking(doc, p);
}
}
} else {
Expand All @@ -104,7 +104,7 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre

if (typeof def !== 'undefined') {
doc_[piece] = def;
doc.$__.activePaths.default(p);
applyChangeTracking(doc, p);
}
}
} else {
Expand All @@ -113,3 +113,14 @@ module.exports = function applyDefaults(doc, fields, exclude, hasIncludedChildre
}
}
};

/*!
* ignore
*/

function applyChangeTracking(doc, fullPath) {
doc.$__.activePaths.default(fullPath);
if (doc.$isSubdocument && doc.$isSingleNested && doc.$parent() != null) {
doc.$parent().$__.activePaths.default(doc.$__pathRelativeToParent(fullPath));
}
}
38 changes: 38 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12110,6 +12110,44 @@ describe('document', function() {
assert.equal(existingTest.isModified(), true);
assert.equal(existingTest.modifiedPaths().length, 1);
});

it('saves single nested subdoc defaults (gh-12905)', async function() {
const nestedSchema = new mongoose.Schema({
refOriginal: String,
refAnother: {
type: String,
default: () => 'goodbye'
}
});
const testSchema = new mongoose.Schema({
original: String,
another: {
type: String,
default: 'hello'
},
referenced: {
type: nestedSchema,
default: () => ({})
}
});
const Test = db.model('Test', testSchema);

const _id = new mongoose.Types.ObjectId();
await Test.collection.insertOne({
_id,
original: 'foo',
referenced: { refOriginal: 'hello' }
});

const doc = await Test.findById(_id);
assert.equal(doc.referenced.refOriginal, 'hello');
assert.equal(doc.referenced.refAnother, 'goodbye');

await doc.save();
const rawDoc = await Test.findById(_id).lean();
assert.equal(rawDoc.referenced.refOriginal, 'hello');
assert.equal(rawDoc.referenced.refAnother, 'goodbye');
});
});

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