Conversation
Contributor
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The new loop unintentionally stops removing undefined fields from branch objects.
Review effort: Balanced
Findings: 1
Open (1)
What changed in this PR
Fixes casting within $expr.$switch branch expressions.
Changes:
- Casts each branch’s
caseandthenexpressions. - Adds regression coverage for comparison and arithmetic casting.
| File | Description |
|---|---|
lib/helpers/query/cast$expr.js |
Recursively casts $switch branch expressions. |
test/helpers/query.cast$expr.test.js |
Tests casting within case and then. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+100
to
+107
| for (const branch of val.$switch.branches) { | ||
| if (branch?.case != null) { | ||
| branch.case = _castExpression(branch.case, schema, strictQuery); | ||
| } | ||
| if (branch?.then != null) { | ||
| branch.then = _castExpression(branch.then, schema, strictQuery); | ||
| } | ||
| } |
Author
There was a problem hiding this comment.
Good catch, thanks. I confirmed that master drops then: undefined from the branch through the final omitUndefined() pass, while this loop kept it. I added omitUndefined(branch) after casting case and then, so branches get the same cleanup as before, and extended the test with a branch that has then: undefined.
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Summary
cast$expr()passes each$switchbranch object straight to_castExpression():A branch is
{ case: <expression>, then: <expression> }._castExpression()only acts on operator keys like$eqor$multiply, so on a branch object it finds nothing to do and returns it unchanged. Thecaseandthenexpressions are never cast, while theif,thenandelseof$condare.This PR casts
branch.caseandbranch.thenthe same way$condcastsifandthen. Missing keys are skipped, so a malformed branch still goes to the server as is.Examples
Added
casts case and then expressions in $switch branchestotest/helpers/query.cast$expr.test.js. It fails onmaster(the'18'incaseand the'2'inthenstay strings) and passes with this change. The existing gh-14751$switchtest and the other$exprtests still pass, and eslint is clean.