Skip to content

fix: use json.Marshal in WriteJSONResponse to avoid trailing newline#3466

Merged
lpcox merged 1 commit intomainfrom
lpcox/fix-writejsonresponse-trailing-newline
Apr 9, 2026
Merged

fix: use json.Marshal in WriteJSONResponse to avoid trailing newline#3466
lpcox merged 1 commit intomainfrom
lpcox/fix-writejsonresponse-trailing-newline

Conversation

@lpcox
Copy link
Copy Markdown
Collaborator

@lpcox lpcox commented Apr 9, 2026

Problem

Commit f9f875b refactored two sites in internal/proxy/handler.go to use httputil.WriteJSONResponse instead of manual w.Write calls. However, WriteJSONResponse used json.NewEncoder(w).Encode(body), which always appends a trailing newline. This caused 8 proxy tests to fail:

  • TestWriteEmptyResponse (6 subtests)
  • TestHandleWithDIFC_LabelResponseError_CoarseBlocked
  • TestHandleWithDIFC_NoFineGrainedLabels_CoarseBlocked

All failures showed the same pattern: expected "[]" but got "[]\n".

Fix

Switch WriteJSONResponse from json.NewEncoder(w).Encode(body) to json.Marshal(body) + w.Write(data), which produces clean JSON without a trailing newline. This is the standard approach for HTTP JSON responses.

Verification

make agent-finished passes — all unit and integration tests green.

json.Encoder.Encode appends a trailing newline character after the JSON
output. This caused 8 proxy tests to fail after commit f9f875b refactored
manual w.Write calls to use WriteJSONResponse.

Switch from json.NewEncoder(w).Encode(body) to json.Marshal(body) +
w.Write(data), which produces clean JSON without a trailing newline.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 9, 2026 18:18
@lpcox lpcox merged commit 81fdf1e into main Apr 9, 2026
20 checks passed
@lpcox lpcox deleted the lpcox/fix-writejsonresponse-trailing-newline branch April 9, 2026 18:19
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes proxy test failures by changing httputil.WriteJSONResponse to emit JSON without the trailing newline produced by json.Encoder.Encode.

Changes:

  • Replace json.NewEncoder(w).Encode(body) with json.Marshal(body) followed by w.Write(data) to avoid a trailing newline.
  • Add early-return handling for marshal failures.
Show a summary per file
File Description
internal/httputil/httputil.go Updates JSON response writing to avoid trailing newline in HTTP responses.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment on lines 13 to +17
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(body)
data, err := json.Marshal(body)
if err != nil {
return
Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json.Marshal is done after WriteHeader(statusCode). If marshaling fails, this returns after headers/status have already been sent, leaving an empty body with the original status code (often 200) and no indication of the server error. Consider marshaling first, and on error writing an explicit 500 (or at least the correct error status) before sending headers; also consider handling/logging the w.Write error.

See below for a potential fix:

	data, err := json.Marshal(body)
	if err != nil {
		http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(statusCode)
	if _, err := w.Write(data); err != nil {
		return
	}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants