Skip to content

Commit a57bb72

Browse files
authored
Merge pull request #13928 from Automattic/vkarpov15/gh-13881
fix(query): handle `$round` in `$expr` as array
2 parents ef012b3 + fd22f9b commit a57bb72

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

lib/helpers/query/cast$expr.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ const arithmeticOperatorNumber = new Set([
2929
'$floor',
3030
'$ln',
3131
'$log10',
32-
'$round',
3332
'$sqrt',
3433
'$sin',
3534
'$cos',
@@ -118,6 +117,13 @@ function _castExpression(val, schema, strictQuery) {
118117
if (val.$size) {
119118
val.$size = castNumberOperator(val.$size, schema, strictQuery);
120119
}
120+
if (val.$round) {
121+
const $round = val.$round;
122+
if (!Array.isArray($round) || $round.length < 1 || $round.length > 2) {
123+
throw new CastError('Array', $round, '$round');
124+
}
125+
val.$round = $round.map(v => castNumberOperator(v, schema, strictQuery));
126+
}
121127

122128
_omitUndefined(val);
123129

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

+10
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,14 @@ describe('castexpr', function() {
108108
const res = cast$expr({ $not: { $in: ['42', '$nums'] } }, testSchema);
109109
assert.deepStrictEqual(res, { $not: { $in: [42, '$nums'] } });
110110
});
111+
112+
it('casts $round (gh-13881)', function() {
113+
const testSchema = new Schema({ value: Number });
114+
115+
let res = cast$expr({ $eq: [{ $round: ['$value', '00'] }, 2] }, testSchema);
116+
assert.deepStrictEqual(res, { $eq: [{ $round: ['$value', 0] }, 2] });
117+
118+
res = cast$expr({ $eq: [{ $round: ['$value'] }, 2] }, testSchema);
119+
assert.deepStrictEqual(res, { $eq: [{ $round: ['$value'] }, 2] });
120+
});
111121
});

0 commit comments

Comments
 (0)