From 83b4947ad2621f6b54c7f7f4a77d26f850169cc6 Mon Sep 17 00:00:00 2001 From: Spencer Torres Date: Sat, 6 Apr 2024 01:46:12 -0400 Subject: [PATCH] fix(frontend): allow "constructor" property in response data (#25407) (cherry picked from commit a1983e468ba1a1b0fdbef9d8d5206e61be0b7141) --- .../src/connection/callApi/parseResponse.ts | 6 +++++- .../test/connection/callApi/parseResponse.test.ts | 12 ++++++++++-- .../src/components/FilterableTable/index.tsx | 7 ++++++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/superset-frontend/packages/superset-ui-core/src/connection/callApi/parseResponse.ts b/superset-frontend/packages/superset-ui-core/src/connection/callApi/parseResponse.ts index 15beca6e150ad..90d348691455e 100644 --- a/superset-frontend/packages/superset-ui-core/src/connection/callApi/parseResponse.ts +++ b/superset-frontend/packages/superset-ui-core/src/connection/callApi/parseResponse.ts @@ -16,11 +16,15 @@ * specific language governing permissions and limitations * under the License. */ -import JSONbig from 'json-bigint'; +import _JSONbig from 'json-bigint'; import { cloneDeepWith } from 'lodash'; import { ParseMethod, TextResponse, JsonResponse } from '../types'; +const JSONbig = _JSONbig({ + constructorAction: 'preserve', +}); + export default async function parseResponse( apiPromise: Promise, parseMethod?: T, diff --git a/superset-frontend/packages/superset-ui-core/test/connection/callApi/parseResponse.test.ts b/superset-frontend/packages/superset-ui-core/test/connection/callApi/parseResponse.test.ts index e13964ecf7300..b08b5b8cb80c4 100644 --- a/superset-frontend/packages/superset-ui-core/test/connection/callApi/parseResponse.test.ts +++ b/superset-frontend/packages/superset-ui-core/test/connection/callApi/parseResponse.test.ts @@ -139,8 +139,12 @@ describe('parseResponse()', () => { it('resolves to big number value if `parseMethod=json-bigint`', async () => { const mockBigIntUrl = '/mock/get/bigInt'; - const mockGetBigIntPayload = - '{ "value": 9223372036854775807, "minus": { "value": -483729382918228373892, "str": "something" }, "number": 1234, "floatValue": { "plus": 0.3452211361231223, "minus": -0.3452211361231223 } }'; + const mockGetBigIntPayload = `{ + "value": 9223372036854775807, "minus": { "value": -483729382918228373892, "str": "something" }, + "number": 1234, "floatValue": { "plus": 0.3452211361231223, "minus": -0.3452211361231223 }, + "string.constructor": "data.constructor", + "constructor": "constructor" + }`; fetchMock.get(mockBigIntUrl, mockGetBigIntPayload); const responseBigNumber = await parseResponse( callApi({ url: mockBigIntUrl, method: 'GET' }), @@ -167,6 +171,10 @@ describe('parseResponse()', () => { expect(Math.abs(responseBigNumber.json.floatValue.minus)).toEqual( responseBigNumber.json.floatValue.plus, ); + expect(responseBigNumber.json['string.constructor']).toEqual( + 'data.constructor', + ); + expect(responseBigNumber.json.constructor).toEqual('constructor'); }); it('rejects if request.ok=false', async () => { diff --git a/superset-frontend/src/components/FilterableTable/index.tsx b/superset-frontend/src/components/FilterableTable/index.tsx index d89a65ad3294d..11400c1ecfbed 100644 --- a/superset-frontend/src/components/FilterableTable/index.tsx +++ b/superset-frontend/src/components/FilterableTable/index.tsx @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -import JSONbig from 'json-bigint'; +import _JSONbig from 'json-bigint'; import React, { useEffect, useRef, useState, useMemo } from 'react'; import { getMultipleTextDimensions, styled } from '@superset-ui/core'; import { useDebounceValue } from 'src/hooks/useDebounceValue'; @@ -24,6 +24,11 @@ import { useCellContentParser } from './useCellContentParser'; import { renderResultCell } from './utils'; import { Table, TableSize } from '../Table'; +const JSONbig = _JSONbig({ + storeAsString: true, + constructorAction: 'preserve', +}); + const SCROLL_BAR_HEIGHT = 15; // This regex handles all possible number formats in javascript, including ints, floats, // exponential notation, NaN, and Infinity.