Skip to content

Commit 81fdf1e

Browse files
authored
fix: use json.Marshal in WriteJSONResponse to avoid trailing newline (#3466)
## 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](https://pkg.go.dev/encoding/json#Encoder.Encode). 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.
2 parents c52441a + 8c44561 commit 81fdf1e

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

internal/httputil/httputil.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,9 @@ import (
1212
func WriteJSONResponse(w http.ResponseWriter, statusCode int, body interface{}) {
1313
w.Header().Set("Content-Type", "application/json")
1414
w.WriteHeader(statusCode)
15-
json.NewEncoder(w).Encode(body)
15+
data, err := json.Marshal(body)
16+
if err != nil {
17+
return
18+
}
19+
w.Write(data)
1620
}

0 commit comments

Comments
 (0)