Skip to content

fix(populate): preserve explicit read option inheritance - #16632

Closed
AbdelrahmanHafez wants to merge 14 commits into
Automattic:masterfrom
AbdelrahmanHafez:fix/populate-clone-read-options
Closed

AbdelrahmanHafez wants to merge 14 commits into
Automattic:masterfrom
AbdelrahmanHafez:fix/populate-clone-read-options

Conversation

@AbdelrahmanHafez

@AbdelrahmanHafez AbdelrahmanHafez commented Oct 23, 2026 •

Copy link
Copy Markdown
Collaborator

re #15567 #15553

Use the latest explicit read settings for populate, including after cloning:

const query = Article.find().populate('author').read('primary');
await query;
await query.clone().read('secondaryPreferred');
// Author should use secondaryPreferred.

Keep schema defaults local to each model:

// Article schema: read = 'secondaryPreferred'; Author schema: read = 'primaryPreferred'.
await Article.find().populate('author');
// Author should keep primaryPreferred.

Explicit query options propagate through nested populate. Populate options override them. Null inherits. The same rules apply to readConcern.

This changes schema-default inheritance introduced in d333d8e in 8.17.1. Applications relying on parent defaults reaching other models will change behavior.

@AbdelrahmanHafez AbdelrahmanHafez changed the title fix(populate): refresh inherited read options on query clones fix(populate): preserve read option inheritance Oct 23, 2026
@AbdelrahmanHafez
AbdelrahmanHafez marked this pull request as ready for review October 23, 2026 15:06
@AbdelrahmanHafez
AbdelrahmanHafez requested review from hasezoey and vkarpov15 and removed request for hasezoey October 23, 2026 15:06

@hasezoey hasezoey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AbdelrahmanHafez
AbdelrahmanHafez marked this pull request as draft October 24, 2026 12:15
@AbdelrahmanHafez

Copy link
Copy Markdown
Collaborator Author

Found a couple more potential issues, investigating...

@AbdelrahmanHafez AbdelrahmanHafez changed the title fix(populate): preserve read option inheritance fix(populate): preserve explicit read option inheritance Oct 25, 2026
@AbdelrahmanHafez
AbdelrahmanHafez marked this pull request as ready for review October 25, 2026 22:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Deferred nested population with function-valued matches still loses explicit read settings.

1 open issue
What changed in this PR

This PR updates populate read-option inheritance to use the latest explicit query settings, including after cloning, while keeping schema defaults local to each model.

Changes:

  • Move read-option inheritance into execution-local populate preparation.
  • Remove inheritance that mutates stored populate options.
  • Expand regression tests for ordering, null inheritance, nested overrides, schema defaults, and clones.
File Description
test/​query.test.js Verify read settings passed to populated queries.
test/​model.populate.test.js Cover defaults, overrides, null values, nested population, and cloning.
lib/​queryHelpers.js Apply explicit read settings to execution-local populate options.
lib/​query.js Remove mutation-based inheritance from execution option preparation.

Review effort: Balanced

Comment thread lib/queryHelpers.js Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes consistently implement the stated inheritance rules with focused regression coverage and no identified blocking issues; runtime tests were not executed.

1 open issue

Review effort: Balanced

@hasezoey hasezoey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vkarpov15 vkarpov15 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not liking the shallow clones, however it does look like there's a test failure without them so I'm digging more into why.

Comment thread lib/queryHelpers.js
// Inherit explicit query settings, keeping schema defaults local to each model.
// Use execution-local options so clones can use new read settings.
for (const key of ['readPreference', 'readConcern']) {
if (query.options[key] && pop[i].options?.[key] == null) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here query.options doesn't go through query's optionsForExec(), which the removed code in lib/query.js did - this means that schema-level defaults for readPreference and readConcern aren't getting applied properly if pop[i].options?.[key] == null

Comment thread lib/queryHelpers.js
} else {
pop[i]._queryProjection = projection;
pop[i]._localModel = query.model;
pop[i] = { ...pop[i], _queryProjection: projection, _localModel: query.model };

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shallow clone shouldn't be necessary, and may be harmful because it converts PopulateOptions instance into a POJO. However, it does look like tests fail without this shallow clone - worth investigating why.

vkarpov15 added a commit that referenced this pull request Nov 8, 2026
vkarpov15 added a commit that referenced this pull request Nov 9, 2026
fix(model+query): handle inherited read preference and read concern on cloned queries
@vkarpov15 vkarpov15 closed this Nov 9, 2026
opensource-fork-sync Bot pushed a commit to cla7aye15I4nd/opensource-Automattic__mongoose that referenced this pull request Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants