Skip to content

Commit dfde779

Browse files
committed
fix(document): allow manually populating path within document array
Fix #8273
1 parent b86749e commit dfde779

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

lib/document.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,20 @@ Document.prototype.$set = function $set(path, val, type, options) {
11671167
val = schema.applySetters(val, this, false, priorVal);
11681168
}
11691169

1170+
if (schema.$isMongooseDocumentArray &&
1171+
Array.isArray(val) &&
1172+
val.length > 0 &&
1173+
val[0].$__ != null &&
1174+
val[0].$__.populated != null) {
1175+
const populatedPaths = Object.keys(val[0].$__.populated);
1176+
for (const populatedPath of populatedPaths) {
1177+
this.populated(path + '.' + populatedPath,
1178+
val.map(v => v.populated(populatedPath)),
1179+
val[0].$__.populated[populatedPath].options);
1180+
}
1181+
didPopulate = true;
1182+
}
1183+
11701184
if (!didPopulate && this.$__.populated) {
11711185
delete this.$__.populated[path];
11721186
}

lib/schematype.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,7 @@ SchemaType._isRef = function(self, value, doc, init) {
12651265
// - setting / pushing values after population
12661266
const path = doc.$__fullPath(self.path);
12671267
const owner = doc.ownerDocument ? doc.ownerDocument() : doc;
1268-
ref = owner.populated(path);
1268+
ref = owner.populated(path) || doc.populated(self.path);
12691269
}
12701270

12711271
if (ref) {

lib/types/documentarray.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class CoreDocumentArray extends CoreMongooseArray {
8686
}
8787
}
8888
}
89-
89+
9090
if (Constructor.$isMongooseDocumentArray) {
9191
return Constructor.cast(value, this, undefined, undefined, index);
9292
}

test/model.discriminator.test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,5 +1508,37 @@ describe('model', function() {
15081508
});
15091509
assert.strictEqual(doc.operations[0].pitchPath.path[0]._id, void 0);
15101510
});
1511+
1512+
it('with discriminators in embedded arrays (gh-8273)', function(done) {
1513+
const ProductSchema = new Schema({
1514+
title: String
1515+
});
1516+
const Product = mongoose.model('gh8273_Product', ProductSchema);
1517+
const ProductItemSchema = new Schema({
1518+
product: { type: Schema.Types.ObjectId, ref: 'gh8273_Product' }
1519+
});
1520+
1521+
const OrderItemSchema = new Schema({}, {discriminatorKey: '__t'});
1522+
1523+
const OrderSchema = new Schema({
1524+
items: [OrderItemSchema],
1525+
});
1526+
1527+
OrderSchema.path('items').discriminator('ProductItem', ProductItemSchema);
1528+
const Order = mongoose.model('Order', OrderSchema);
1529+
1530+
const product = new Product({title: 'Product title'});
1531+
1532+
const order = new Order({
1533+
items: [{
1534+
__t: 'ProductItem',
1535+
product: product
1536+
}]
1537+
});
1538+
assert.ok(order.items[0].product.title);
1539+
assert.equal(order.populated('items.product').length, 1);
1540+
1541+
done();
1542+
});
15111543
});
15121544
});

0 commit comments

Comments
 (0)