Skip to content

Commit

Permalink
fix(document): apply getters on array elements when calling `toObject…
Browse files Browse the repository at this point in the history
…({ getters: true })`

Re: #9889
  • Loading branch information
vkarpov15 committed Feb 12, 2021
1 parent 1e9eda3 commit b301374
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/schema/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,12 +261,19 @@ SchemaArray.prototype.enum = function() {
*/

SchemaArray.prototype.applyGetters = function(value, scope) {
if (this.caster.options && this.caster.options.ref) {
if (scope != null && scope.populated(this.path)) {
// means the object id was populated
return value;
}

return SchemaType.prototype.applyGetters.call(this, value, scope);
const ret = SchemaType.prototype.applyGetters.call(this, value, scope);
if (Array.isArray(ret)) {
const len = ret.length;
for (let i = 0; i < len; ++i) {
ret[i] = this.caster.applyGetters(ret[i], scope);
}
}
return ret;
};

SchemaArray.prototype._applySetters = function(value, scope, init, priorVal) {
Expand Down
8 changes: 8 additions & 0 deletions lib/schema/documentarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ DocumentArrayPath.prototype.clone = function() {
return schematype;
};

/*!
* ignore
*/

DocumentArrayPath.prototype.applyGetters = function(value, scope) {
return SchemaType.prototype.applyGetters.call(this, value, scope);
};

/*!
* Scopes paths selected in a query to this array.
* Necessary for proper default application of subdocument values.
Expand Down

0 comments on commit b301374

Please sign in to comment.