Skip to content

fix(utils): wrap discriminator merge check in parentheses to fix precedence#16351

Merged
vkarpov15 merged 1 commit into
Automattic:7.xfrom
greymoth-jp:fix/utils-discriminator-merge-precedence-7x
Jul 6, 2026
Merged

fix(utils): wrap discriminator merge check in parentheses to fix precedence#16351
vkarpov15 merged 1 commit into
Automattic:7.xfrom
greymoth-jp:fix/utils-discriminator-merge-precedence-7x

Conversation

@greymoth-jp

Copy link
Copy Markdown

Backports #16187 to the 7.x line.

utils.merge() guards against merging a single-nested path against a document-array path (and vice versa) when building a discriminator schema (gh-9534). Because && binds tighter than ||, the condition

options.isDiscriminatorSchemaMerge &&
(from[key].$isSingleNested && to[key].$isMongooseDocumentArray) ||
(from[key].$isMongooseDocumentArray && to[key].$isSingleNested)

parses as (A && B) || C, so the continue also fires when isDiscriminatorSchemaMerge is false and from is a document array while to is single-nested. The path is then silently dropped from a normal (non-discriminator) merge. Wrapping the two clauses restores A && (B || C).

The 7.x utils.merge() body is identical to master apart from this grouping, so the fix and its test port over unchanged.

Verified on 7.x: the isDiscriminatorSchemaMerge: false test fails before the change (undefined !== 2) and passes after; test/utils.test.js stays green (26 passing).

…edence

Backport of Automattic#16187 to 7.x. Because && binds tighter than ||, the
isDiscriminatorSchemaMerge guard parsed as (A && B) || C, so merge() also
skipped a single-nested vs document-array path on a normal merge where
isDiscriminatorSchemaMerge is false. Wrapping the clauses restores A && (B || C).

Re: Automatticgh-9534
@vkarpov15 vkarpov15 added this to the 7.8.11 milestone Jul 6, 2026
@vkarpov15 vkarpov15 requested a review from Copilot July 6, 2026 18:43

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.

Pull request overview

Backports a fix to utils.merge() in the 7.x line to ensure the discriminator-specific schema merge guard is correctly gated by options.isDiscriminatorSchemaMerge, preventing unintended key skipping in normal merges.

Changes:

  • Fix JavaScript operator-precedence in utils.merge() by wrapping the discriminator mismatch check in parentheses.
  • Add regression tests ensuring the mismatch-skip behavior only applies when isDiscriminatorSchemaMerge: true.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
lib/utils.js Wrapes the single-nested vs document-array mismatch condition so it’s fully guarded by isDiscriminatorSchemaMerge.
test/utils.test.js Adds regression coverage for both discriminator and non-discriminator merge behavior for the mismatch case.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@vkarpov15 vkarpov15 merged commit 199b63b into Automattic:7.x Jul 6, 2026
14 checks passed
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