Skip to content

Commit

Permalink
fix(document): call subdocument getters if child schema has getters: …
Browse files Browse the repository at this point in the history
…true

Fix #12105
  • Loading branch information
vkarpov15 committed Jul 25, 2022
1 parent edcf468 commit abb3563
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/document.js
Expand Up @@ -3642,10 +3642,10 @@ Document.prototype.$toObject = function(options, json) {
options.minimize = _minimize;

cloneOptions._parentOptions = options;
cloneOptions._skipSingleNestedGetters = true;
cloneOptions._skipSingleNestedGetters = false;

const gettersOptions = Object.assign({}, cloneOptions);
gettersOptions._skipSingleNestedGetters = false;
gettersOptions._skipSingleNestedGetters = true;

// remember the root transform function
// to save it from being overwritten by sub-transform functions
Expand Down
23 changes: 22 additions & 1 deletion test/document.test.js
Expand Up @@ -5426,8 +5426,29 @@ describe('document', function() {

doc.toObject({ getters: false });
assert.equal(called, 1);
});

return Promise.resolve();
it('calls subdocument getters if child schema has getters: true (gh-12105)', function() {
let called = 0;

const childSchema = new Schema({
_id: false,
value: {
type: String,
get: function(v) {
++called;
return v.toUpperCase();
}
}
}, { toJSON: { getters: true } });
const schema = new Schema({ name: childSchema });
const Test = db.model('Test', schema);

const doc = new Test({ name: { value: 'John Smith' } });

const res = doc.toJSON();
assert.equal(called, 1);
assert.deepStrictEqual(res.name, { value: 'JOHN SMITH' });
});

it('setting doc array to array of top-level docs works (gh-5632)', function(done) {
Expand Down

0 comments on commit abb3563

Please sign in to comment.