mirror of
https://github.com/Kong/insomnia.git
synced 2026-10-07 05:26:45 -04:00
## Background
The environment editor's JSON view had two reproducible problems.
**1. Deleting all JSON content left the Table view unchanged**
1. Open an environment and configure several variables in **Table View**.
2. Switch to **JSON View**.
3. Delete all content in the JSON editor.
4. Switch back to **Table View**.
Expected: the variable entries are gone, since the JSON content is empty. Actual: every previously configured entry is still listed — the deletion is ignored.
**2. Switching routes and coming back dropped the error state**
1. Clear the JSON view: the editor shows `Unexpected end of JSON input` and switching to Table view is blocked by the error dialog.
2. Navigate to another route, then return to this page.
Expected: the state stays consistent. Actual: the error is gone and the empty document can be switched to Table view, which again shows the last saved variables.
**Root cause**
- `EnvironmentEditor.getValue()` short-circuited on an empty document (`if (!editorRef.current || !value) return null`), so `onChange` never fired: no error was recorded, nothing was submitted, and `isValid()` stayed `true`. On the JSON → Table switch, `useToggleEnvironmentType` then derived `kvPairData` from the unchanged `environment.data` — the last saved variables.
- Validity was push-only: it was refreshed exclusively from `CodeEditor`'s debounced `onChange`. On remount the `CodeEditor` restores unsaved content from its `historyKey` cache programmatically (cached-state `setValue` inside `initEditor`, before the `changes` listener is attached), so no `onChange` ran and the error silently vanished.
## Changes
**`packages/insomnia/src/ui/components/editors/environment-editor.tsx`**
- `getValue()` no longer special-cases empty content: an empty (or whitespace-only) document reaches `orderedJSON.parse`, which throws, so it takes exactly the same error path as any other malformed JSON (inline notice + `isValid() === false`).
- Validation is one `validate()` helper (parse + `checkNestedKeys`) shared by `onChange` and by a new `useEffect` that re-applies it on mount and whenever the incoming value or `historyKey` changes, so the notice and the validity follow the document instead of the last edit event.
- `isValid()` is derived from the document on demand, so it cannot lag the `CodeEditor`'s 100 ms `onChange` debounce.
- `getValue()` is documented as throwing on invalid content; every caller gates on `isValid()`, and `request-group-pane` additionally wraps it in `try/catch`.
**`packages/insomnia-smoke-test/tests/smoke/environment-editor-interactions.test.ts`**
- New regression test: clear the JSON content, assert the parse error surfaces and that switching to Table view is blocked by the existing error dialog; then close and reopen the editor and assert the error state survives the remount and the switch stays blocked.
## Other
- **Product decision**: an empty document is treated as invalid JSON and follows the existing malformed-JSON handling (inline notice + the existing dialog that blocks the switch). Interpreting it as `{}` was rejected because it would wipe every variable on a transient select-all + delete.
- **Behavior changes**: empty content reports an error and blocks the JSON ↔ Table switch; an invalid document keeps its error across remounts and route changes until it is fixed; `getValue()` may throw for invalid content.
- **Unchanged**: a top-level JSON value whose object is falsy (`false`, `0`, `""`) keeps its pre-existing silent no-op behavior — neither widened nor narrowed.
- **Verification**:
- The new test fails on the pre-fix source (missing error notice, switch not blocked, KV list still visible) and passes with the fix.
- `npm run test:smoke:dev -- environment-editor-interactions`: 6 passed.
- `npm run type-check` clean, `npm run lint` 0 errors, `npx prettier --check` clean.
- `npm test -w packages/insomnia`: only the pre-existing, unrelated `git-service-clone-folder-naming` failure, which also fails on clean `develop`.
[INS-3884](https://konghq.atlassian.net/browse/INS-3884)