From 24c28bbc2fd74cae5f35d7ded2a33a2a60f95562 Mon Sep 17 00:00:00 2001 From: Yzzz <16530207+yi-shunlong@user.noreply.gitee.com> Date: Mon, 23 Nov 2026 12:29:25 +0800 Subject: [PATCH] fix(cli): preserve multi-record responses in raw API envelopes --- cli/cmd/api/api.go | 2 +- cli/cmd/api/api_test.go | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cli/cmd/api/api.go b/cli/cmd/api/api.go index b01993d84e3..7054caf495f 100644 --- a/cli/cmd/api/api.go +++ b/cli/cmd/api/api.go @@ -413,7 +413,7 @@ func emitRawBody(body []byte, fopts *cmdutil.FormatOptions) error { // digits through instead. decoder := json.NewDecoder(bytes.NewReader(body)) decoder.UseNumber() - if err := decoder.Decode(&bodyAny); err != nil { + if err := decoder.Decode(&bodyAny); err != nil || !json.Valid(body) { bodyAny = string(body) } } diff --git a/cli/cmd/api/api_test.go b/cli/cmd/api/api_test.go index 8fd89a1009f..3de912e8443 100644 --- a/cli/cmd/api/api_test.go +++ b/cli/cmd/api/api_test.go @@ -709,3 +709,21 @@ func TestAPI_POST_NotGated(t *testing.T) { t.Error("POST handler not called - POST must not be gated") } } + +func TestRawEnvelopePreservesNDJSON(t *testing.T) { + out, _ := iostreams.SetForTest(t) + body := []byte("{\"page\":1}\n{\"page\":2}\n") + opts := &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON} + if err := emitRawBody(body, opts); err != nil { + t.Fatal(err) + } + var envelope struct { + Data string `json:"data"` + } + if err := json.Unmarshal(out.Bytes(), &envelope); err != nil { + t.Fatal(err) + } + if envelope.Data != string(body) { + t.Errorf("response lost bytes: %q", envelope.Data) + } +}