Skip to content

Commit fdc7c7e

Browse files
authored
Merge pull request #14761 from Automattic/vkarpov15/gh-14755-backport
fix(query): handle casting $switch in $expr
2 parents e8b0933 + 0c11d12 commit fdc7c7e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/helpers/query/cast$expr.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,12 @@ function _castExpression(val, schema, strictQuery) {
9292
} else if (val.$ifNull != null) {
9393
val.$ifNull.map(v => _castExpression(v, schema, strictQuery));
9494
} else if (val.$switch != null) {
95-
val.branches.map(v => _castExpression(v, schema, strictQuery));
96-
val.default = _castExpression(val.default, schema, strictQuery);
95+
if (Array.isArray(val.$switch.branches)) {
96+
val.$switch.branches = val.$switch.branches.map(v => _castExpression(v, schema, strictQuery));
97+
}
98+
if ('default' in val.$switch) {
99+
val.$switch.default = _castExpression(val.$switch.default, schema, strictQuery);
100+
}
97101
}
98102

99103
const keys = Object.keys(val);

test/helpers/query.cast$expr.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,33 @@ describe('castexpr', function() {
118118
res = cast$expr({ $eq: [{ $round: ['$value'] }, 2] }, testSchema);
119119
assert.deepStrictEqual(res, { $eq: [{ $round: ['$value'] }, 2] });
120120
});
121+
122+
it('casts $switch (gh-14751)', function() {
123+
const testSchema = new Schema({
124+
name: String,
125+
scores: [Number]
126+
});
127+
const res = cast$expr({
128+
$eq: [
129+
{
130+
$switch: {
131+
branches: [{ case: { $eq: ['$$NOW', '$$NOW'] }, then: true }],
132+
default: false
133+
}
134+
},
135+
true
136+
]
137+
}, testSchema);
138+
assert.deepStrictEqual(res, {
139+
$eq: [
140+
{
141+
$switch: {
142+
branches: [{ case: { $eq: ['$$NOW', '$$NOW'] }, then: true }],
143+
default: false
144+
}
145+
},
146+
true
147+
]
148+
});
149+
});
121150
});

0 commit comments

Comments
 (0)