Skip to content

Commit 6579f93

Browse files
authored
Merge pull request #14335 from Automattic/vkarpov15/gh-14315
fix: include virtuals in document array `toString()` output if `toObject.virtuals` set
2 parents 2c34102 + 0643edd commit 6579f93

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

lib/document.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -4287,7 +4287,8 @@ Document.prototype.inspect = function(options) {
42874287
opts = options;
42884288
opts.minimize = false;
42894289
}
4290-
const ret = this.toObject(opts);
4290+
4291+
const ret = arguments.length > 0 ? this.toObject(opts) : this.toObject();
42914292

42924293
if (ret == null) {
42934294
// If `toObject()` returns null, `this` is still an object, so if `inspect()`

lib/schema.js

+12
Original file line numberDiff line numberDiff line change
@@ -2144,6 +2144,18 @@ Schema.prototype.set = function(key, value, tags) {
21442144
if (key === 'strictQuery') {
21452145
_propagateOptionsToImplicitlyCreatedSchemas(this, { strictQuery: value });
21462146
}
2147+
if (key === 'toObject') {
2148+
value = { ...value };
2149+
// Avoid propagating transform to implicitly created schemas re: gh-3279
2150+
delete value.transform;
2151+
_propagateOptionsToImplicitlyCreatedSchemas(this, { toObject: value });
2152+
}
2153+
if (key === 'toJSON') {
2154+
value = { ...value };
2155+
// Avoid propagating transform to implicitly created schemas re: gh-3279
2156+
delete value.transform;
2157+
_propagateOptionsToImplicitlyCreatedSchemas(this, { toJSON: value });
2158+
}
21472159

21482160
return this;
21492161
};

lib/types/documentArray/methods/index.js

+11
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const arrayPathSymbol = require('../../../helpers/symbols').arrayPathSymbol;
1313
const arraySchemaSymbol = require('../../../helpers/symbols').arraySchemaSymbol;
1414
const documentArrayParent = require('../../../helpers/symbols').documentArrayParent;
1515

16+
const _baseToString = Array.prototype.toString;
17+
1618
const methods = {
1719
/*!
1820
* ignore
@@ -22,6 +24,15 @@ const methods = {
2224
return this.toObject(internalToObjectOptions);
2325
},
2426

27+
toString() {
28+
return _baseToString.call(this.__array.map(subdoc => {
29+
if (subdoc != null && subdoc.$__ != null) {
30+
return subdoc.toString();
31+
}
32+
return subdoc;
33+
}));
34+
},
35+
2536
/*!
2637
* ignore
2738
*/

lib/types/subdocument.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -400,11 +400,7 @@ Subdocument.prototype.populate = function() {
400400
*/
401401

402402
Subdocument.prototype.inspect = function() {
403-
return this.toObject({
404-
transform: false,
405-
virtuals: false,
406-
flattenDecimals: false
407-
});
403+
return this.toObject();
408404
};
409405

410406
if (util.inspect.custom) {

test/document.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -12953,6 +12953,25 @@ describe('document', function() {
1295312953
};
1295412954
assert.equal(person.address.zip, 54321);
1295512955
});
12956+
12957+
it('includes virtuals in doc array toString() output if virtuals enabled on toObject (gh-14315)', function() {
12958+
const schema = new Schema({
12959+
docArr: [{ childId: mongoose.ObjectId }]
12960+
});
12961+
schema.virtual('docArr.child', { ref: 'Child', localField: 'docArr.childId', foreignField: '_id' });
12962+
schema.set('toObject', { virtuals: true });
12963+
schema.set('toJSON', { virtuals: true });
12964+
const Test = db.model('Test', schema);
12965+
const Child = db.model('Child', new Schema({
12966+
name: String
12967+
}));
12968+
12969+
const child = new Child({ name: 'test child' });
12970+
const doc = new Test({ docArr: [{ childId: child._id }] });
12971+
doc.docArr[0].child = child;
12972+
assert.ok(doc.docArr.toString().includes('child'), doc.docArr.toString());
12973+
assert.ok(doc.docArr.toString().includes('test child'), doc.docArr.toString());
12974+
});
1295612975
});
1295712976

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

0 commit comments

Comments
 (0)