fix(output): stop HTML-escaping JSON output - #100
Closed
Priyanshu (priyanshu-plivo) wants to merge 3 commits into
Closed
Priyanshu (priyanshu-plivo) wants to merge 3 commits into
Priyanshu (priyanshu-plivo) wants to merge 3 commits into
Conversation
encoding/json escapes <, > and & as unicode sequences by default. In -o json and piped output that garbled hints such as `participant add <name>` and every & in an API URL, so the bytes no longer matched what the API returned. JSONRaw had the same problem: the encoder re-compacts the raw upstream body and escapes it on the way through. JSONSuccess, JSONRaw and JSONError now share one encoder with HTML escaping off; indentation and the trailing newline are unchanged.
The shared writers no longer escape <, > and &, but several commands still build JSON with encoding/json before it reaches the user: - agents list --all merges pages by re-encoding them, so names and pagination URLs came out escaped - the -o json result of the SIP write commands - ask -o json, which streams each server event as a JSON line - the ask and auth token mint --dry-run request previews - the JSON pass-through of auth token list (internal build) output.Marshal and output.MarshalIndent are json.Marshal and json.MarshalIndent with HTML escaping off, and these sites now use them. Formatting is otherwise byte-identical.
--dry-run and --log-level debug print request bodies through the redactor, which re-encodes them, so every API command showed a text like "Tom & Jerry <3" escaped. An error message built from an unrecognized error body had the same problem. Both now use output.Marshal. Only what is displayed changes: request bodies sent to the API are encoded as before.
Contributor
Author
|
Superseded by #101, which combines this change with the login paste fallback. |
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.
What
Go's JSON encoding HTML-escapes
<,>and&into unicode escape sequences. The CLI's JSON output, error envelopes and dry-run prints all showed those escapes:participant add <name>printed with escaped brackets.&weren't byte-for-byte what the API returned.--text 'Tom & Jerry <3'dry-run showed escaped characters.It's valid JSON, but noisy for people and agents reading the raw output.
Change
internal/output:JSONSuccessandJSONErroruse one encoder with HTML escaping off (SetEscapeHTML(false)).JSONRawgoes throughJSONSuccess, so raw API responses are fixed too.output.Marshalandoutput.MarshalIndentbehave like theencoding/jsonequivalents with escaping off.agentsmulti-page merge,sipwrite output (mustJSON),ask -o json, and theaskandauth tokendry-run prints.--dry-runand--log-level debug.encoding/jsonfound no differences apart from the three characters.Tests
mustJSON, the multi-page merge (a page-2 name and page 1'snextURL with&), theask -o jsonrenderer, an offlineask --dry-run, the redactor, and the error fallback all failed on the old code with escaped output and pass now.go test -race ./...,golangci-lint run, builds for windows and-tags internal,make docsdrift, andscripts/smoke.sh.