You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: parsing big integers in JSON data loses precision #489
Describe the Bug
When parsing JSON data, if a JSON field value is a number, and that number is really big, JavaScript loses precision. For example, the Twitter API exposes the Tweet ID. This is a large number that exceeds the integer space of Number. You can see this by simply assigning a large number to a simple variable (it's not actually JSON that is the problem, it's JavaScript itself).
> let id = 1524831183200260097
undefined
> id
1524831183200260000
I think your Steps to Reproduce are a little misleading. You type there in verbatim a long number. JavaScript engine, parse a file, and use IEEE 754 Standard to parse a Number. You could avoid that by using BigInt notation:
The problem exists, though. It lies in parsers implemented in this library. Here's simple jest for HTTP:
const{HTTP}=require('cloudevents')test('Parse big numbers from HTTP',()=>{letce=HTTP.toEvent({headers: {'content-type': 'application/cloudevents+json'},body: `{"id": 1524831183200260097}`})expect(ce.id).toBe(1524831183200260097n);})
Activity
cardil commentedon May 13, 2022
I think your Steps to Reproduce are a little misleading. You type there in verbatim a long number. JavaScript engine, parse a file, and use IEEE 754 Standard to parse a
Number. You could avoid that by usingBigIntnotation:The problem exists, though. It lies in parsers implemented in this library. Here's simple jest for
HTTP:This test fails:
github-actions commentedon Jun 13, 2022
This issue is stale because it has been open 30 days with no activity.
lance commentedon Jun 14, 2022
@cardil you are correct to point out the error in my example. But your example would not produce a valid CE even if we could handle big integers, because the required type for the
idfield isString. https://github.com/cloudevents/spec/blob/main/cloudevents/spec.md#idI think this is still a problem, but if something is sending a big int for the
idthat's not going to fly.fix: handle big integers in incoming events
fix: handle big integers in incoming events
github-actions commentedon Jul 15, 2022
This issue is stale because it has been open 30 days with no activity.
fix: handle big integers in incoming events
fix: handle big integers in incoming events (#495)