-
Notifications
You must be signed in to change notification settings - Fork 863
Warn before opening a very large JSON value in the data grid (#9868) #10075
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
Merged
asheshv
merged 3 commits into
pgadmin-org:master
from
dpage:worktree-fix-issue-9868-large-json-warning
Jun 12, 2026
+196
−1
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
web/pgadmin/tools/sqleditor/static/js/components/QueryToolDataGrid/json_editor_warning.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| ///////////////////////////////////////////////////////////// | ||
| // | ||
| // pgAdmin 4 - PostgreSQL Tools | ||
| // | ||
| // Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| // This software is released under the PostgreSQL Licence | ||
| // | ||
| ////////////////////////////////////////////////////////////// | ||
|
|
||
| // Opening a very large JSON/JSONB value in the cell editor parses, pretty- | ||
| // prints and renders the whole document on the main thread, which can make | ||
| // pgAdmin slow or completely unresponsive. Above this size (in characters) we | ||
| // warn the user and let them decide whether to proceed. See issue #9868. | ||
| export const LARGE_JSON_WARNING_LENGTH = 1024 * 1024; | ||
|
|
||
| // Returns true when the raw cell value is large enough that opening it in the | ||
| // JSON editor warrants a confirmation prompt. Only string values are measured; | ||
| // the cheap length check avoids serializing the value just to size it. | ||
| export function shouldWarnForLargeJSON(value, threshold = LARGE_JSON_WARNING_LENGTH) { | ||
| return typeof value === 'string' && value.length > threshold; | ||
| } |
134 changes: 134 additions & 0 deletions
134
web/regression/javascript/sqleditor/json_editor_warning.spec.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| ///////////////////////////////////////////////////////////// | ||
| // | ||
| // pgAdmin 4 - PostgreSQL Tools | ||
| // | ||
| // Copyright (C) 2013 - 2026, The pgAdmin Development Team | ||
| // This software is released under the PostgreSQL Licence | ||
| // | ||
| ////////////////////////////////////////////////////////////// | ||
|
|
||
| import { render, screen, act } from '@testing-library/react'; | ||
|
|
||
| import { | ||
| LARGE_JSON_WARNING_LENGTH, | ||
| shouldWarnForLargeJSON, | ||
| } from '../../../pgadmin/tools/sqleditor/static/js/components/QueryToolDataGrid/json_editor_warning'; | ||
|
|
||
| // Stub the heavy JSON editor so the wrapper can render without CodeMirror. | ||
| jest.mock('../../../pgadmin/static/js/components/JsonEditor', () => ({ | ||
| __esModule: true, | ||
| default: () => <div data-testid="json-editor" />, | ||
| })); | ||
|
|
||
| // Mock the QueryToolDataGrid index so importing Editors does not pull in the | ||
| // whole data grid; Editors only needs RowInfoContext from it. | ||
| jest.mock('../../../pgadmin/tools/sqleditor/static/js/components/QueryToolDataGrid', () => { | ||
| const ReactActual = require('react'); | ||
| return { RowInfoContext: ReactActual.createContext() }; | ||
| }); | ||
|
|
||
| import Theme from 'sources/Theme'; | ||
| import { JsonTextEditor } from '../../../pgadmin/tools/sqleditor/static/js/components/QueryToolDataGrid/Editors'; | ||
| import { RowInfoContext } from '../../../pgadmin/tools/sqleditor/static/js/components/QueryToolDataGrid'; | ||
| import { PgAdminProvider } from '../../../pgadmin/static/js/PgAdminProvider'; | ||
|
|
||
| describe('QueryToolDataGrid json_editor_warning', () => { | ||
| describe('shouldWarnForLargeJSON', () => { | ||
| it('does not warn for small string values', () => { | ||
| expect(shouldWarnForLargeJSON('{"a":1}')).toBe(false); | ||
| expect(shouldWarnForLargeJSON('')).toBe(false); | ||
| }); | ||
|
|
||
| it('warns once a string value exceeds the threshold (#9868)', () => { | ||
| const big = 'x'.repeat(LARGE_JSON_WARNING_LENGTH + 1); | ||
| expect(shouldWarnForLargeJSON(big)).toBe(true); | ||
| }); | ||
|
|
||
| it('does not warn exactly at the threshold', () => { | ||
| const atLimit = 'x'.repeat(LARGE_JSON_WARNING_LENGTH); | ||
| expect(shouldWarnForLargeJSON(atLimit)).toBe(false); | ||
| }); | ||
|
|
||
| it('respects a custom threshold', () => { | ||
| expect(shouldWarnForLargeJSON('abcd', 3)).toBe(true); | ||
| expect(shouldWarnForLargeJSON('abc', 3)).toBe(false); | ||
| }); | ||
|
|
||
| it('does not warn for non-string values', () => { | ||
| expect(shouldWarnForLargeJSON(null)).toBe(false); | ||
| expect(shouldWarnForLargeJSON(undefined)).toBe(false); | ||
| expect(shouldWarnForLargeJSON(12345)).toBe(false); | ||
| expect(shouldWarnForLargeJSON([1, 2, 3])).toBe(false); | ||
| expect(shouldWarnForLargeJSON({ a: 1 })).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('JsonTextEditor large-value guard', () => { | ||
| const KEY = 'col'; | ||
| let confirmArgs; | ||
| let onClose; | ||
|
|
||
| const renderEditor = (value) => { | ||
| const column = { key: KEY, idx: 0, can_edit: true }; | ||
| const pgAdmin = { | ||
| Browser: { | ||
| notifier: { | ||
| confirm: (title, text, onOk, onCancel) => { | ||
| confirmArgs = { title, text, onOk, onCancel }; | ||
| }, | ||
| error: jest.fn(), | ||
| }, | ||
| }, | ||
| }; | ||
| return render( | ||
| <Theme> | ||
| <PgAdminProvider value={pgAdmin}> | ||
| <RowInfoContext.Provider value={{ getCellElement: () => null }}> | ||
| <JsonTextEditor | ||
| row={{ [KEY]: value }} | ||
| column={column} | ||
| onRowChange={jest.fn()} | ||
| onClose={onClose} | ||
| /> | ||
| </RowInfoContext.Provider> | ||
| </PgAdminProvider> | ||
| </Theme> | ||
| ); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| confirmArgs = undefined; | ||
| onClose = jest.fn(); | ||
| }); | ||
|
|
||
| it('opens the editor immediately for small values without confirming', () => { | ||
| renderEditor('{"a":1}'); | ||
| expect(confirmArgs).toBeUndefined(); | ||
| expect(screen.getByTestId('json-editor')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('warns for large values and waits before opening the editor', () => { | ||
| renderEditor('x'.repeat(LARGE_JSON_WARNING_LENGTH + 1)); | ||
| expect(confirmArgs).toBeDefined(); | ||
| expect(screen.queryByTestId('json-editor')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('closes the editor when the warning is cancelled', () => { | ||
| renderEditor('x'.repeat(LARGE_JSON_WARNING_LENGTH + 1)); | ||
| act(() => confirmArgs.onCancel()); | ||
| expect(onClose).toHaveBeenCalledWith(false); | ||
| }); | ||
|
|
||
| it('opens the editor and does not close it when the user continues (#9868)', () => { | ||
| renderEditor('x'.repeat(LARGE_JSON_WARNING_LENGTH + 1)); | ||
| // notifier.confirm() fires the cancel callback as well as the OK | ||
| // callback when the user clicks OK; the guard must keep the editor open. | ||
| act(() => { | ||
| confirmArgs.onOk(); | ||
| confirmArgs.onCancel(); | ||
| }); | ||
| expect(onClose).not.toHaveBeenCalled(); | ||
| expect(screen.getByTestId('json-editor')).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.