Skip to content

Commit

Permalink
Merge pull request #14335 from Automattic/vkarpov15/gh-14315
Browse files Browse the repository at this point in the history
fix: include virtuals in document array `toString()` output if `toObject.virtuals` set
  • Loading branch information
vkarpov15 authored Feb 8, 2024
2 parents 2c34102 + 0643edd commit 6579f93
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -4287,7 +4287,8 @@ Document.prototype.inspect = function(options) {
opts = options;
opts.minimize = false;
}
const ret = this.toObject(opts);

const ret = arguments.length > 0 ? this.toObject(opts) : this.toObject();

if (ret == null) {
// If `toObject()` returns null, `this` is still an object, so if `inspect()`
Expand Down
12 changes: 12 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -2144,6 +2144,18 @@ Schema.prototype.set = function(key, value, tags) {
if (key === 'strictQuery') {
_propagateOptionsToImplicitlyCreatedSchemas(this, { strictQuery: value });
}
if (key === 'toObject') {
value = { ...value };
// Avoid propagating transform to implicitly created schemas re: gh-3279
delete value.transform;
_propagateOptionsToImplicitlyCreatedSchemas(this, { toObject: value });
}
if (key === 'toJSON') {
value = { ...value };
// Avoid propagating transform to implicitly created schemas re: gh-3279
delete value.transform;
_propagateOptionsToImplicitlyCreatedSchemas(this, { toJSON: value });
}

return this;
};
Expand Down
11 changes: 11 additions & 0 deletions lib/types/documentArray/methods/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const arrayPathSymbol = require('../../../helpers/symbols').arrayPathSymbol;
const arraySchemaSymbol = require('../../../helpers/symbols').arraySchemaSymbol;
const documentArrayParent = require('../../../helpers/symbols').documentArrayParent;

const _baseToString = Array.prototype.toString;

const methods = {
/*!
* ignore
Expand All @@ -22,6 +24,15 @@ const methods = {
return this.toObject(internalToObjectOptions);
},

toString() {
return _baseToString.call(this.__array.map(subdoc => {
if (subdoc != null && subdoc.$__ != null) {
return subdoc.toString();
}
return subdoc;
}));
},

/*!
* ignore
*/
Expand Down
6 changes: 1 addition & 5 deletions lib/types/subdocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -400,11 +400,7 @@ Subdocument.prototype.populate = function() {
*/

Subdocument.prototype.inspect = function() {
return this.toObject({
transform: false,
virtuals: false,
flattenDecimals: false
});
return this.toObject();
};

if (util.inspect.custom) {
Expand Down
19 changes: 19 additions & 0 deletions test/document.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12953,6 +12953,25 @@ describe('document', function() {
};
assert.equal(person.address.zip, 54321);
});

it('includes virtuals in doc array toString() output if virtuals enabled on toObject (gh-14315)', function() {
const schema = new Schema({
docArr: [{ childId: mongoose.ObjectId }]
});
schema.virtual('docArr.child', { ref: 'Child', localField: 'docArr.childId', foreignField: '_id' });
schema.set('toObject', { virtuals: true });
schema.set('toJSON', { virtuals: true });
const Test = db.model('Test', schema);
const Child = db.model('Child', new Schema({
name: String
}));

const child = new Child({ name: 'test child' });
const doc = new Test({ docArr: [{ childId: child._id }] });
doc.docArr[0].child = child;
assert.ok(doc.docArr.toString().includes('child'), doc.docArr.toString());
assert.ok(doc.docArr.toString().includes('test child'), doc.docArr.toString());
});
});

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

0 comments on commit 6579f93

Please sign in to comment.