Skip to content
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

Handle BigInt numbers from API #920

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "react-app"
"extends": "react-app",
"env": {
"es2020": true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Otherwise, BigInt constructor gives an ESLint error 🤦

}
}
2 changes: 1 addition & 1 deletion packages/react-api/__tests__/api/lds.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ describe('lds', () => {
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => {
return {
ok: true,
json: async () => [{ value: [someCoordinates] }]
text: async () => JSON.stringify({ value: [someCoordinates] })
};
}));

Expand Down
23 changes: 19 additions & 4 deletions packages/react-api/__tests__/api/stats.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('stats', () => {
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => {
return {
ok: true,
json: async () => TABLE_TEST.output
text: async () => JSON.stringify(TABLE_TEST.output)
};
}));

Expand Down Expand Up @@ -117,7 +117,7 @@ describe('stats', () => {
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => {
return {
ok: true,
json: async () => QUERY_TEST.output
text: async () => JSON.stringify(QUERY_TEST.output)
};
}));

Expand Down Expand Up @@ -149,7 +149,7 @@ describe('stats', () => {
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => {
return {
ok: true,
json: async () => QUERY_TEST.output
text: async () => JSON.stringify(QUERY_TEST.output)
};
}));

Expand Down Expand Up @@ -187,7 +187,7 @@ describe('stats', () => {
const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => {
return {
ok: true,
json: async () => QUERY_TEST.output
text: async () => JSON.stringify(QUERY_TEST.output)
};
}));

Expand Down Expand Up @@ -225,5 +225,20 @@ describe('stats', () => {
expect(res).toEqual(TILESET_TEST.output);
expect(mockedGetTileJson).toBeCalledWith({ source: TILESET_TEST.input.source });
});

test('BigInt numbers are correctly parsed', async () => {
const jsonWithBigInt = '{"rows": [{"quadbin":5256683984082960383}]}';

const fetchMock = (global.fetch = jest.fn().mockImplementation(async () => {
return {
ok: true,
text: async () => jsonWithBigInt
};
}));

const res = await getStats({ source: QUERY_SOURCE, column: 'injuries' });

expect(res.rows[0].quadbin).toEqual(BigInt(5256683984082960383));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Careful here! Writing this out as BigInt(5256683984082960383) will truncate the number before it's passed to the BigInt function, so these are not equal, note the n suffix:

BigInt(5256683984082960383) === BigInt(5256683984082960383n) // false!

});
});
});
12 changes: 11 additions & 1 deletion packages/react-api/src/api/common.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { InvalidColumnError } from '@carto/react-core/';

// Widgets API may return BigInt values, and JSON.parse does not handle them properly, so we need to use a reviver
const bigIntReviver = (key, value) => {
if (typeof value === 'number' && value > Number.MAX_SAFE_INTEGER) {
return BigInt(value);
}
return value;
};
Comment on lines +4 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above — unfortunately I think that it's necessary to receive the value as a string and not an integer. By the time the reviver receives a numeric value, I think it will be too late to recover the original integer.

Possible solution in https://stackoverflow.com/a/69644630, though maybe this is complex enough that we want a dependency to help...


/**
* Return more descriptive error from API
*/
Expand Down Expand Up @@ -51,7 +59,9 @@ export async function makeCall({ url, credentials, opts }) {
signal: opts?.abortController?.signal,
...opts?.otherOptions
});
data = await response.json();

const text = await response.text();
data = JSON.parse(text, bigIntReviver);
} catch (error) {
if (error.name === 'AbortError') throw error;

Expand Down
Loading