-
-
Notifications
You must be signed in to change notification settings - Fork 275
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: json-bigint-patch causes CASL ability.can() to fail with BigInt conditions #1016
Comments
json-bigint-patch adds custom toJSON method. Ucsst expects toJSON to return primitive value, but in that patch lib it returns non primitive. I think it actually returns BigInt objectified instance. I’ll double check tomorrow |
Hi, |
I've just tested the functionality in ucast/mongo2js and it works with native BigInt. This means it's not a bug but enhancement because you request to make casl work with the patched version of BigInt provided by Then I'm wondering why do you even want to use this patched version. You could use custom serializer + reviver in JSON functions. For example: const num = BigInt('9223372036854775807')
const serializedBigInt = JSON.stringify(num , (key, value) => {
return typeof value === 'bigint' ? `BigInt(${value.toString()})` : value
});
const parsedBigInt = JSON.parse(serializedBigInt, (key, value) => {
return typeof value === 'string' && value.startsWith('BigInt(') ? BigInt(value.slice(7, -1)) : value
}); |
I wanted to share my experience dealing with large numbers in GraphQL and how it relates to this issue. Initially, I followed the recommendation in the GraphQL Scalars BigInt docs to install json-bigint-patch. However, after understanding more about how serialization works, I realized it wasn’t necessary. Instead, I replaced it with the following approach, which ensures proper serialization without needing the additional library:
This automatically converts BigInt values to strings during JSON serialization and works seamlessly with the GraphQL server. It simplifies the setup, and so far, I’ve had no issues with large number precision in queries or responses. I thought this could be a cleaner solution for others facing similar issues. Let me know your thoughts on this approach! Thanks again for all your work on CASL. |
There is one drawback by embedding this into |
I will change logic then, instead of relying on duck typing, I will rely on duck typing only if |
Describe the bug
When importing json-bigint-patch, the ability check in CASL fails to correctly match conditions using BigInt. Specifically, ability.can() incorrectly returns false when it should return true.
To Reproduce
Steps to reproduce the behavior:
Using ability.can() with a condition that should match, but it fails when json-bigint-patch is imported.
Expected behavior
When json-bigint-patch is imported, ability.can() should return true for matching conditions, just as it does without the import.
CASL Version
@casl/ability
- v6.7.3Environment:
The text was updated successfully, but these errors were encountered: