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: include virtuals in document array toString() output if toObject.virtuals set #14335

Merged
merged 2 commits into from Feb 8, 2024
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
3 changes: 2 additions & 1 deletion lib/document.js
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
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
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
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
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