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: BIGINT rendering regression in chartAction #21937

Merged
merged 3 commits into from
Oct 26, 2022
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
18 changes: 9 additions & 9 deletions superset-frontend/package-lock.json

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

2 changes: 1 addition & 1 deletion superset-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@
"@types/jest": "^26.0.3",
"@types/jquery": "^3.5.8",
"@types/js-levenshtein": "^1.1.0",
"@types/json-bigint": "^1.0.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
15 changes: 8 additions & 7 deletions superset-frontend/packages/superset-ui-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
},
"devDependencies": {
"@emotion/styled": "^11.3.0",
"resize-observer-polyfill": "1.5.1",
"fetch-mock": "^6.5.2",
"jest-mock-console": "^1.0.0"
"jest-mock-console": "^1.0.0",
"resize-observer-polyfill": "1.5.1"
},
"dependencies": {
"@babel/runtime": "^7.1.2",
Expand All @@ -38,14 +38,15 @@
"@types/d3-scale": "^2.1.1",
"@types/d3-time": "^3.0.0",
"@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",
"@types/prop-types": "^15.7.2",
"@types/rison": "0.0.6",
"@types/seedrandom": "^2.4.28",
"@types/fetch-mock": "^7.3.3",
"@types/enzyme": "^3.10.5",
"@types/prop-types": "^15.7.2",
"@vx/responsive": "^0.0.199",
"csstype": "^2.6.4",
"d3-format": "^1.3.2",
Expand All @@ -72,8 +73,8 @@
"@types/react": "*",
"@types/react-loadable": "*",
"@types/tinycolor2": "*",
"tinycolor2": "*",
"react": "^16.13.1",
"react-loadable": "^5.5.0"
"react-loadable": "^5.5.0",
"tinycolor2": "*"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/
import JSONbig from 'json-bigint';

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

Expand All @@ -25,7 +26,7 @@ export default async function parseResponse<T extends ParseMethod = 'json'>(
) {
type ReturnType = T extends 'raw' | null
? Response
: T extends 'json' | undefined
: T extends 'json' | 'json-bigint' | undefined
? JsonResponse
: T extends 'text'
? TextResponse
Expand All @@ -46,6 +47,15 @@ export default async function parseResponse<T extends ParseMethod = 'json'>(
};
return result as ReturnType;
}
if (parseMethod === 'json-bigint') {
const rawData = await response.text();
const json = JSONbig.parse(rawData);
const result: JsonResponse = {
response,
json,
};
return result as ReturnType;
}
// by default treat this as json
if (parseMethod === undefined || parseMethod === 'json') {
const json = await response.json();
Expand All @@ -56,6 +66,6 @@ export default async function parseResponse<T extends ParseMethod = 'json'>(
return result as ReturnType;
}
throw new Error(
`Expected parseResponse=json|text|raw|null, got '${parseMethod}'.`,
`Expected parseResponse=json|json-bigint|text|raw|null, got '${parseMethod}'.`,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,13 @@ export type Method = RequestInit['method'];
export type Mode = RequestInit['mode'];
export type Redirect = RequestInit['redirect'];
export type ClientTimeout = number | undefined;
export type ParseMethod = 'json' | 'text' | 'raw' | null | undefined;
export type ParseMethod =
| 'json'
| 'json-bigint'
| 'text'
| 'raw'
| null
| undefined;
export type Signal = RequestInit['signal'];
export type Stringify = boolean;
export type Url = string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ describe('parseResponse()', () => {
expect(responseRaw.bodyUsed).toBe(false);
});

it('resolves to big number value if `parseMethod=json-bigint`', async () => {
const mockBigIntUrl = '/mock/get/bigInt';
const mockGetBigIntPayload = '{ "value": 9223372036854775807 }';
fetchMock.get(mockBigIntUrl, mockGetBigIntPayload);
const responseBigNumber = await parseResponse(
callApi({ url: mockBigIntUrl, method: 'GET' }),
'json-bigint',
);
expect(`${responseBigNumber.json.value}`).toEqual('9223372036854775807');
});
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for adding the test!

Copy link
Member

Choose a reason for hiding this comment

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

Do we also mock a -2^63 to test int64? and we also need to add a comment to point out the integer type is that singed int64.

Copy link
Member Author

Choose a reason for hiding this comment

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

@zhaoyongjie good point. I added the spec for negative number.


it('rejects if request.ok=false', async () => {
expect.assertions(3);
const mockNotOkayUrl = '/mock/notokay/url';
Expand Down
15 changes: 5 additions & 10 deletions superset-frontend/src/SqlLab/actions/sqlLab.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* under the License.
*/
import shortid from 'shortid';
import JSONbig from 'json-bigint';
import { t, SupersetClient } from '@superset-ui/core';
import invert from 'lodash/invert';
import mapKeys from 'lodash/mapKeys';
Expand Down Expand Up @@ -306,12 +305,9 @@ export function fetchQueryResults(query, displayLimit) {

return SupersetClient.get({
endpoint: `/superset/results/${query.resultsKey}/?rows=${displayLimit}`,
parseMethod: 'text',
parseMethod: 'json-bigint',
})
.then(({ text = '{}' }) => {
const bigIntJson = JSONbig.parse(text);
return dispatch(querySuccess(query, bigIntJson));
})
.then(({ json }) => dispatch(querySuccess(query, json)))
.catch(response =>
getClientErrorObject(response).then(error => {
const message =
Expand Down Expand Up @@ -352,12 +348,11 @@ export function runQuery(query) {
endpoint: `/superset/sql_json/${search}`,
body: JSON.stringify(postPayload),
headers: { 'Content-Type': 'application/json' },
parseMethod: 'text',
parseMethod: 'json-bigint',
})
.then(({ text = '{}' }) => {
.then(({ json }) => {
if (!query.runAsync) {
const bigIntJson = JSONbig.parse(text);
dispatch(querySuccess(query, bigIntJson));
dispatch(querySuccess(query, json));
}
})
.catch(response =>
Expand Down
2 changes: 2 additions & 0 deletions superset-frontend/src/components/Chart/chartAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ const legacyChartDataRequest = async (
...requestParams,
url,
postPayload: { form_data: formData },
parseMethod: 'json-bigint',
};

const clientMethod =
Expand Down Expand Up @@ -196,6 +197,7 @@ const v1ChartDataRequest = async (
url,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload),
parseMethod: 'json-bigint',
};

return SupersetClient.post(querySettings);
Expand Down
38 changes: 38 additions & 0 deletions superset-frontend/src/components/Chart/chartActions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,25 @@ describe('chart actions', () => {
);
expect(dispatch.args[0][0].type).toBe(actions.CHART_UPDATE_STARTED);
});

it('should handle the bigint without regression', async () => {
getChartDataUriStub.restore();
const mockBigIntUrl = '/mock/chart/data/bigint';
const expectedBigNumber = '9223372036854775807';
fetchMock.post(mockBigIntUrl, `{ "value": ${expectedBigNumber} }`, {
overwriteRoutes: true,
});
getChartDataUriStub = sinon
.stub(exploreUtils, 'getChartDataUri')
.callsFake(() => URI(mockBigIntUrl));

const { json } = await actions.getChartDataRequest({
formData: fakeMetadata,
});

expect(fetchMock.calls(mockBigIntUrl)).toHaveLength(1);
expect(json.value.toString()).toEqual(expectedBigNumber);
});
});

describe('legacy API', () => {
Expand Down Expand Up @@ -194,5 +213,24 @@ describe('chart actions', () => {
setupDefaultFetchMock();
});
});

it('should handle the bigint without regression', async () => {
getExploreUrlStub.restore();
const mockBigIntUrl = '/mock/chart/data/bigint';
const expectedBigNumber = '9223372036854775807';
fetchMock.post(mockBigIntUrl, `{ "value": ${expectedBigNumber} }`, {
overwriteRoutes: true,
});
getExploreUrlStub = sinon
.stub(exploreUtils, 'getExploreUrl')
.callsFake(() => mockBigIntUrl);

const { json } = await actions.getChartDataRequest({
formData: fakeMetadata,
});

expect(fetchMock.calls(mockBigIntUrl)).toHaveLength(1);
expect(json.result[0].value.toString()).toEqual(expectedBigNumber);
});
});
});