Skip to content

Commit

Permalink
fix(schema): handle bitwise operators on Int32
Browse files Browse the repository at this point in the history
Fix #15170
  • Loading branch information
vkarpov15 committed Jan 11, 2025
1 parent 39886fb commit 0cad0d7
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/schema/bigint.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ SchemaBigInt.prototype.castForQuery = function($conditional, val, context) {
return handler.call(this, val);
}

return this.applySetters(null, val, context);
return this.applySetters(val, context);
}

try {
Expand Down
2 changes: 1 addition & 1 deletion lib/schema/boolean.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ SchemaBoolean.prototype.castForQuery = function($conditional, val, context) {
return handler.call(this, val);
}

return this.applySetters(null, val, context);
return this.applySetters(val, context);
}

try {
Expand Down
9 changes: 7 additions & 2 deletions lib/schema/int32.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
const CastError = require('../error/cast');
const SchemaType = require('../schemaType');
const castInt32 = require('../cast/int32');
const handleBitwiseOperator = require('./operators/bitwise');

/**
* Int32 SchemaType constructor.
Expand Down Expand Up @@ -200,7 +201,11 @@ SchemaInt32.$conditionalHandlers = {
$gt: handleSingle,
$gte: handleSingle,
$lt: handleSingle,
$lte: handleSingle
$lte: handleSingle,
$bitsAllClear: handleBitwiseOperator,
$bitsAnyClear: handleBitwiseOperator,
$bitsAllSet: handleBitwiseOperator,
$bitsAnySet: handleBitwiseOperator
};

/*!
Expand Down Expand Up @@ -228,7 +233,7 @@ SchemaInt32.prototype.castForQuery = function($conditional, val, context) {
return handler.call(this, val);
}

return this.applySetters(null, val, context);
return this.applySetters(val, context);
}

try {
Expand Down
12 changes: 12 additions & 0 deletions test/cast.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ describe('cast: ', function() {
{ x: { $bitsAnyClear: Buffer.from([3]) } });
});

it('with int32 (gh-15170)', function() {
const schema = new Schema({ x: 'Int32' });
assert.deepEqual(cast(schema, { x: { $bitsAnySet: 3 } }),
{ x: { $bitsAnySet: 3 } });
});

it('throws when invalid', function() {
const schema = new Schema({ x: Number });
assert.throws(function() {
Expand Down Expand Up @@ -250,4 +256,10 @@ describe('cast: ', function() {
'state.fieldFoo': '44'
});
});

it('treats unknown operators as passthrough (gh-15170)', function() {
const schema = new Schema({ x: Boolean });
assert.deepEqual(cast(schema, { x: { $someConditional: true } }),
{ x: { $someConditional: true } });
});
});

0 comments on commit 0cad0d7

Please sign in to comment.