From bb2853c9686caec1a45446d1ecafeca69b5d4606 Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 21 Jul 2024 13:54:15 -0400 Subject: [PATCH 1/2] fix(query): handle casting $switch in $expr Fix #14751 --- lib/helpers/query/cast$expr.js | 4 ++-- test/helpers/query.cast$expr.test.js | 29 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/helpers/query/cast$expr.js b/lib/helpers/query/cast$expr.js index 9889d47ada1..d91110049f4 100644 --- a/lib/helpers/query/cast$expr.js +++ b/lib/helpers/query/cast$expr.js @@ -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); diff --git a/test/helpers/query.cast$expr.test.js b/test/helpers/query.cast$expr.test.js index 416db9af5c6..fd3f7cd5bfe 100644 --- a/test/helpers/query.cast$expr.test.js +++ b/test/helpers/query.cast$expr.test.js @@ -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 + ] + }); + }); }); From 08e65f179c9da34109db5d41e63d3a08e44dd3df Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sun, 21 Jul 2024 13:57:06 -0400 Subject: [PATCH 2/2] fix: better edge case handling --- lib/helpers/query/cast$expr.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/helpers/query/cast$expr.js b/lib/helpers/query/cast$expr.js index d91110049f4..8e84011b2c3 100644 --- a/lib/helpers/query/cast$expr.js +++ b/lib/helpers/query/cast$expr.js @@ -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.$switch.branches = val.$switch.branches.map(v => _castExpression(v, schema, strictQuery)); - val.$switch.default = _castExpression(val.$switch.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);