Skip to content

Commit

Permalink
fix(query): handle casting $switch in $expr
Browse files Browse the repository at this point in the history
Fix #14751
Backport #14755 to 7.x
  • Loading branch information
vkarpov15 committed Jul 23, 2024
1 parent 5a71c3e commit 0c11d12
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 @@ -92,8 +92,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 0c11d12

Please sign in to comment.