Skip to content

Commit

Permalink
fix: invalid float number format by json-bigint (#21996)
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark committed Nov 2, 2022
1 parent a9b229d commit 3a02339
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 24 deletions.
52 changes: 41 additions & 11 deletions superset-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion superset-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
"jquery": "^3.5.1",
"js-levenshtein": "^1.1.6",
"js-yaml-loader": "^1.2.2",
"json-bigint-native": "^1.2.0",
"json-bigint": "^1.0.0",
"json-stringify-pretty-compact": "^2.0.0",
"lodash": "^4.17.21",
"lodash-es": "^4.17.21",
Expand Down Expand Up @@ -247,6 +247,7 @@
"@types/jest": "^26.0.3",
"@types/jquery": "^3.5.8",
"@types/js-levenshtein": "^1.1.0",
"@types/json-bigint": "^1.0.1",
"@types/react": "^16.9.43",
"@types/react-dom": "^16.9.8",
"@types/react-gravatar": "^2.6.8",
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/packages/superset-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"@types/d3-time-format": "^2.1.0",
"@types/enzyme": "^3.10.5",
"@types/fetch-mock": "^7.3.3",
"@types/json-bigint": "^1.0.1",
"@types/lodash": "^4.14.149",
"@types/math-expression-evaluator": "^1.2.1",
"@types/node": "^18.0.0",
Expand All @@ -55,7 +56,6 @@
"d3-time-format": "^2.2.0",
"fetch-retry": "^4.0.1",
"jed": "^1.1.1",
"json-bigint-native": "^1.2.0",
"lodash": "^4.17.11",
"math-expression-evaluator": "^1.3.8",
"pretty-ms": "^7.0.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
* specific language governing permissions and limitations
* under the License.
*/
import JSONbig from 'json-bigint-native';
import JSONbig from 'json-bigint';
import { cloneDeepWith } from 'lodash';

import { ParseMethod, TextResponse, JsonResponse } from '../types';

Expand Down Expand Up @@ -52,7 +53,11 @@ export default async function parseResponse<T extends ParseMethod = 'json'>(
const json = JSONbig.parse(rawData);
const result: JsonResponse = {
response,
json,
// `json-bigint` could not handle floats well, see sidorares/json-bigint#62
// TODO: clean up after json-bigint>1.0.1 is released
json: cloneDeepWith(json, (value: any) =>
value?.isInteger?.() === false ? Number(value) : undefined,
),
};
return result as ReturnType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,27 +140,33 @@ describe('parseResponse()', () => {
it('resolves to big number value if `parseMethod=json-bigint`', async () => {
const mockBigIntUrl = '/mock/get/bigInt';
const mockGetBigIntPayload =
'{ "value": 9223372036854775807, "minusValue": -483729382918228373892, "number": 1234, "floatValue": 0.345221136, "minusFloatValue": -0.345221136 }';
'{ "value": 9223372036854775807, "minus": { "value": -483729382918228373892, "str": "something" }, "number": 1234, "floatValue": { "plus": 0.3452211361231223, "minus": -0.3452211361231223 } }';
fetchMock.get(mockBigIntUrl, mockGetBigIntPayload);
const responseBigNumber = await parseResponse(
callApi({ url: mockBigIntUrl, method: 'GET' }),
'json-bigint',
);
expect(`${responseBigNumber.json.value}`).toEqual('9223372036854775807');
expect(`${responseBigNumber.json.minusValue}`).toEqual(
expect(`${responseBigNumber.json.minus.value}`).toEqual(
'-483729382918228373892',
);
expect(responseBigNumber.json.number).toEqual(1234);
expect(responseBigNumber.json.floatValue).toEqual(0.345221136);
expect(responseBigNumber.json.minusFloatValue).toEqual(-0.345221136);
expect(responseBigNumber.json.floatValue.plus).toEqual(0.3452211361231223);
expect(responseBigNumber.json.floatValue.minus).toEqual(
-0.3452211361231223,
);
expect(
responseBigNumber.json.floatValue +
responseBigNumber.json.minusFloatValue,
responseBigNumber.json.floatValue.plus +
responseBigNumber.json.floatValue.minus,
).toEqual(0);
expect(
responseBigNumber.json.floatValue /
responseBigNumber.json.minusFloatValue,
responseBigNumber.json.floatValue.plus /
responseBigNumber.json.floatValue.minus,
).toEqual(-1);
expect(Math.min(responseBigNumber.json.floatValue.plus, 0)).toEqual(0);
expect(Math.abs(responseBigNumber.json.floatValue.minus)).toEqual(
responseBigNumber.json.floatValue.plus,
);
});

it('rejects if request.ok=false', async () => {
Expand Down
2 changes: 1 addition & 1 deletion superset-frontend/src/components/FilterableTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import JSONbig from 'json-bigint-native';
import JSONbig from 'json-bigint';
import React, { useEffect, useRef, useState } from 'react';
import JSONTree from 'react-json-tree';
import {
Expand Down

0 comments on commit 3a02339

Please sign in to comment.