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
  • Loading branch information
vkarpov15 committed Jul 21, 2024
1 parent e4462c6 commit bb2853c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/helpers/query/cast$expr.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ 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);
val.$switch.branches = val.$switch.branches.map(v => _castExpression(v, schema, strictQuery));
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 bb2853c

Please sign in to comment.