Skip to content

Commit

Permalink
Merge pull request #13002 from Automattic/vkarpov15/gh-12905
Browse files Browse the repository at this point in the history
fix(document): save newly set defaults underneath single nested subdocuments
  • Loading branch information
vkarpov15 committed Feb 6, 2023
2 parents 5099423 + d69c25f commit 1643461
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
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

0 comments on commit 1643461

Please sign in to comment.