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: mark documents that are populated using hydratedPopulatedDocs option as populated in top-level doc #15080

Merged
merged 4 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,14 @@ function init(self, obj, doc, opts, prefix) {
reason: e
}));
}
} else if (opts.hydratedPopulatedDocs) {
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
doc[i] = schemaType.cast(value, self, true);

if (doc[i] && doc[i].$__ && doc[i].$__.wasPopulated) {
self.$populated(path, doc[i].$__.wasPopulated.value, doc[i].$__.wasPopulated.options);
} else if (Array.isArray(doc[i]) && doc[i].length && doc[i][0]?.$__?.wasPopulated) {
self.$populated(path, doc[i].map(populatedDoc => populatedDoc?.$__?.wasPopulated?.value).filter(val => val != null), doc[i][0].$__.wasPopulated.options);
}
} else {
doc[i] = value;
}
Expand Down
5 changes: 3 additions & 2 deletions lib/schemaType.js
Original file line number Diff line number Diff line change
Expand Up @@ -1567,8 +1567,9 @@ SchemaType.prototype._castRef = function _castRef(value, doc, init) {
!doc.$__.populated[path].options ||
!doc.$__.populated[path].options.options ||
!doc.$__.populated[path].options.options.lean) {
ret = new pop.options[populateModelSymbol](value);
ret.$__.wasPopulated = { value: ret._doc._id };
const PopulatedModel = pop ? pop.options[populateModelSymbol] : doc.constructor.db.model(this.options.ref);
ret = new PopulatedModel(value);
ret.$__.wasPopulated = { value: ret._doc._id, options: { [populateModelSymbol]: PopulatedModel } };
}

return ret;
Expand Down
28 changes: 26 additions & 2 deletions test/model.hydrate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ describe('model', function() {
users: [{ ref: 'User', type: Schema.Types.ObjectId }]
});

db.model('UserTestHydrate', userSchema);
const Company = db.model('CompanyTestHyrdrate', companySchema);
db.deleteModel(/User/);
db.deleteModel(/Company/);
db.model('User', userSchema);
const Company = db.model('Company', companySchema);

const users = [{ _id: new mongoose.Types.ObjectId(), name: 'Val' }];
const company = { _id: new mongoose.Types.ObjectId(), name: 'Booster', users: [users[0]] };
Expand Down Expand Up @@ -144,6 +146,7 @@ describe('model', function() {
count: true
});

db.deleteModel(/User/);
const User = db.model('User', UserSchema);
const Story = db.model('Story', StorySchema);

Expand Down Expand Up @@ -173,5 +176,26 @@ describe('model', function() {

assert.strictEqual(hydrated.storiesCount, 2);
});

it('sets hydrated docs as populated (gh-15048)', async function() {
const userSchema = new Schema({
name: String
});
const companySchema = new Schema({
name: String,
users: [{ ref: 'User', type: Schema.Types.ObjectId }]
});

db.deleteModel(/User/);
vkarpov15 marked this conversation as resolved.
Show resolved Hide resolved
const User = db.model('User', userSchema);
const Company = db.model('Company', companySchema);

const users = [{ _id: new mongoose.Types.ObjectId(), name: 'Val' }];
const company = { _id: new mongoose.Types.ObjectId(), name: 'Acme', users: [users[0]] };

const c = Company.hydrate(company, null, { hydratedPopulatedDocs: true });
assert.ok(c.populated('users'));
assert.ok(c.users[0] instanceof User);
});
});
});
Loading