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(sqllab): Overflow bigint in json-tree view #22609

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { ReactWrapper } from 'enzyme';
import { styledMount as mount } from 'spec/helpers/theming';
import FilterableTable, {
MAX_COLUMNS_FOR_TABLE,
renderBigIntStrToNumber,
} from 'src/components/FilterableTable';
import { render, screen } from 'spec/helpers/testing-library';
import userEvent from '@testing-library/user-event';
Expand Down Expand Up @@ -331,3 +332,19 @@ describe('FilterableTable sorting - RTL', () => {
expect(gridCells[6]).toHaveTextContent('2022-01-02');
});
});

test('renders bigInt value in a number format', () => {
expect(renderBigIntStrToNumber('123')).toBe('123');
expect(renderBigIntStrToNumber('some string value')).toBe(
'some string value',
);
expect(renderBigIntStrToNumber('{ a: 123 }')).toBe('{ a: 123 }');
expect(renderBigIntStrToNumber('"Not a Number"')).toBe('"Not a Number"');
// trim quotes for bigint string format
expect(renderBigIntStrToNumber('"-12345678901234567890"')).toBe(
'-12345678901234567890',
);
expect(renderBigIntStrToNumber('"12345678901234567890"')).toBe(
'12345678901234567890',
);
});
17 changes: 15 additions & 2 deletions superset-frontend/src/components/FilterableTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function safeJsonObjectParse(

// We know `data` is a string starting with '{' or '[', so try to parse it as a valid object
try {
const jsonData = JSON.parse(data);
const jsonData = JSONbig({ storeAsString: true }).parse(data);
if (jsonData && typeof jsonData === 'object') {
return jsonData;
}
Expand All @@ -63,6 +63,13 @@ function safeJsonObjectParse(
}
}

export function renderBigIntStrToNumber(value: string) {
if (typeof value === 'string' && /^"-?\d+"$/.test(value)) {
return value.substring(1, value.length - 1);
}
return value;
}

const GRID_POSITION_ADJUSTMENT = 4;
const SCROLL_BAR_HEIGHT = 15;
// This regex handles all possible number formats in javascript, including ints, floats,
Expand Down Expand Up @@ -405,7 +412,13 @@ const FilterableTable = ({
jsonString: CellDataType,
) => (
<ModalTrigger
modalBody={<JSONTree data={jsonObject} theme={getJsonTreeTheme()} />}
modalBody={
<JSONTree
data={jsonObject}
theme={getJsonTreeTheme()}
valueRenderer={renderBigIntStrToNumber}
/>
}
modalFooter={
<Button>
<CopyToClipboard shouldShowText={false} text={jsonString} />
Expand Down