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

fix: invalid float number format by json-bigint #21996

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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,10 +16,18 @@
* 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';

// eslint-disable-next-line consistent-return
function parseFloatBigNumber(value: any) {
if (value?.isInteger?.() === false) {
return Number(value);
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Can we just inline this?

      // `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 instanceof BigNumber && !value.isInteger() ? Number(value) : value)
      )),


export default async function parseResponse<T extends ParseMethod = 'json'>(
apiPromise: Promise<Response>,
parseMethod?: T,
Expand Down Expand Up @@ -52,7 +60,8 @@ export default async function parseResponse<T extends ParseMethod = 'json'>(
const json = JSONbig.parse(rawData);
const result: JsonResponse = {
response,
json,
// TODO: keep it until json-bigint@1.0.1 to ignore bignumber.js for float numbers
json: cloneDeepWith(json, parseFloatBigNumber),
};
return result as ReturnType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ 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, "minusValue": -483729382918228373892, "number": 1234, "floatValue": 0.3452211361231223, "minusFloatValue": -0.3452211361231223 }';
fetchMock.get(mockBigIntUrl, mockGetBigIntPayload);
const responseBigNumber = await parseResponse(
callApi({ url: mockBigIntUrl, method: 'GET' }),
Expand All @@ -151,8 +151,8 @@ describe('parseResponse()', () => {
'-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).toEqual(0.3452211361231223);
expect(responseBigNumber.json.minusFloatValue).toEqual(-0.3452211361231223);
expect(
responseBigNumber.json.floatValue +
responseBigNumber.json.minusFloatValue,
Expand All @@ -161,6 +161,10 @@ describe('parseResponse()', () => {
responseBigNumber.json.floatValue /
responseBigNumber.json.minusFloatValue,
).toEqual(-1);
expect(Math.min(responseBigNumber.json.floatValue, 0)).toEqual(0);
expect(Math.abs(responseBigNumber.json.minusFloatValue)).toEqual(
responseBigNumber.json.floatValue,
);
});

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