Skip to content

Commit

Permalink
fix: cannot handle BIGINT in chartAction
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpark committed Oct 25, 2022
1 parent 792820e commit 8dbea45
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 30 deletions.
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');
});

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

0 comments on commit 8dbea45

Please sign in to comment.