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
Manually populating doesn't work for embedded discriminators in embedded array #8273
Comments
I've tried to debug this a bit: here's my unit test (i added an other working it.only('with discriminators in embedded arrays (gh-8273)', function(done) {
const ProductSchema = new Schema({
title: String,
});
const Product = mongoose.model('gh8273_Product', ProductSchema);
const ProductItemSchema = new Schema({
product: { type: Schema.Types.ObjectId, ref: 'gh8273_Product' },
});
const OrderItemSchema = new Schema({}, {discriminatorKey: '__t'});
const OrderSchema = new Schema({
product: { type: Schema.Types.ObjectId, ref: 'gh8273_Product' },
items: [OrderItemSchema],
});
OrderSchema.path('items').discriminator('ProductItem', ProductItemSchema);
const Order = mongoose.model('Order', OrderSchema);
const product = new Product({title: 'Product title'});
const order = new Order({
product,
items: [
{
product,
__t: 'ProductItem',
},
]
});
console.log(order);
assert.ok(order.items[0].product.title);
done();
}); I've isolated it to the fact that during the setting process, the subdocument is casted to the object id ref, which is supposed to check if it's populated to, i guess, either allow the document to pass through or to just keep the id. Cast calls The check fails and this set operation is casted to the not populated version. |
From what I got the populated state is stored in the "local" scope of the embedded item here Lines 1132 to 1135 in 9549015
Then here Lines 1266 to 1268 in 9549015
The population is checked against the full path ( |
@vkarpov15 anything I can do to help from there? I feel i'm lacking a deep understanding of how things work to submit a patch |
Also, this seems very similar to #3575 |
@saveman71 thanks for the test script, that was very helpful. You were right about the |
In the meantime, the fix for this issue will be in 5.7.8 |
@saveman71 yes it will be run, but |
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Manually populated as described in the docs doesn't work for discriminators.
Repro script (sorry a bit long):
What are the versions of Node.js, Mongoose and MongoDB you are using?
Node.js v8.11.0
Mongoose v5.7.6
The text was updated successfully, but these errors were encountered: