fix: read live request body from editor DOM instead of debounced ref - #176
Open
hongwei1 wants to merge 2 commits into
Open
fix: read live request body from editor DOM instead of debounced ref#176hongwei1 wants to merge 2 commits into
hongwei1 wants to merge 2 commits into
Conversation
The JSON request-body editor (json-editor-vue / vanilla-jsoneditor) syncs its content into the value used on submit via onChange, which is debounced (defaults to 300ms, and still resolves asynchronously even at debounce=0). Submitting immediately after editing the body could race ahead of that sync and send stale or empty content, surfacing as a misleading JSON-format error from the API even though the editor showed valid JSON. Read the request body straight from the CodeMirror editor's DOM instead, which is updated synchronously on every edit, with a fallback to the existing ref if the editor node isn't found.
CodeMirror virtualises rendering for long content -- only the visible viewport is actually present in the DOM -- so getCurrentRequestBodyText() reading .cm-content directly (from the previous fix in this branch) could return truncated, invalid JSON for a sufficiently long example body. That invalid JSON then failed JSON.parse() in the request layer, which silently omits the body on a parse failure, sending no body at all. Validate the DOM read is parseable before trusting it; fall back to the exampleRequestBody ref otherwise, which for an unedited pre-filled example still holds the complete text from the API response untouched by CodeMirror's rendering.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
The JSON request-body editor (
json-editor-vue/vanilla-jsoneditor, text mode) syncs its content into the value actually sent on POST/PUT viaonChange, which is debounced (defaults to 300ms, and still resolves asynchronously even withdebounceset to0). Submitting immediately after typing or programmatically setting the body can race ahead of that sync and send stale or empty content — the outgoing request ends up with no body at all, which the API then rejects with a JSON-format error that has nothing to do with the JSON actually shown in the editor.Confirmed by capturing the outgoing request: with no delay after editing,
postDatawasnull; only after waiting past the debounce did it carry the full JSON.Fix
Read the request body straight from the CodeMirror editor's DOM (
.cm-content) at submit time instead of relying on the debouncedexampleRequestBodyref. The editor's own DOM is updated synchronously on every edit, so this sidesteps the race entirely. Falls back to the existing ref if the editor node isn't found for any reason.Update: a second, related bug surfaced during verification. CodeMirror virtualises rendering for long content — only the visible viewport is actually present in the DOM — so the DOM read above can come back truncated (and therefore invalid JSON) for a sufficiently long, unedited example body, even though the editor's actual document is complete. The truncated JSON then failed
JSON.parse()downstream, which silently omits the body on a parse failure rather than erroring. Fixed by validating the DOM read is parseable before trusting it, falling back to theexampleRequestBodyref otherwise — which for an unedited pre-filled example still holds the complete text from the API response, untouched by CodeMirror's rendering.Test plan