Skip to content

Commit

Permalink
Merge pull request #14755 from Automattic/vkarpov15/gh-14751
Browse files Browse the repository at this point in the history
fix(query): handle casting $switch in $expr
  • Loading branch information
vkarpov15 authored Jul 23, 2024
2 parents e4462c6 + 08e65f1 commit 8be1ae8
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/helpers/query/cast$expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,12 @@ function _castExpression(val, schema, strictQuery) {
} else if (val.$ifNull != null) {
val.$ifNull.map(v => _castExpression(v, schema, strictQuery));
} else if (val.$switch != null) {
val.branches.map(v => _castExpression(v, schema, strictQuery));
val.default = _castExpression(val.default, schema, strictQuery);
if (Array.isArray(val.$switch.branches)) {
val.$switch.branches = val.$switch.branches.map(v => _castExpression(v, schema, strictQuery));
}
if ('default' in val.$switch) {
val.$switch.default = _castExpression(val.$switch.default, schema, strictQuery);
}
}

const keys = Object.keys(val);
Expand Down
29 changes: 29 additions & 0 deletions test/helpers/query.cast$expr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,33 @@ describe('castexpr', function() {
res = cast$expr({ $eq: [{ $round: ['$value'] }, 2] }, testSchema);
assert.deepStrictEqual(res, { $eq: [{ $round: ['$value'] }, 2] });
});

it('casts $switch (gh-14751)', function() {
const testSchema = new Schema({
name: String,
scores: [Number]
});
const res = cast$expr({
$eq: [
{
$switch: {
branches: [{ case: { $eq: ['$$NOW', '$$NOW'] }, then: true }],
default: false
}
},
true
]
}, testSchema);
assert.deepStrictEqual(res, {
$eq: [
{
$switch: {
branches: [{ case: { $eq: ['$$NOW', '$$NOW'] }, then: true }],
default: false
}
},
true
]
});
});
});

0 comments on commit 8be1ae8

Please sign in to comment.