Skip to content

[duplicate-code] Duplicate Code Pattern: Manual JSON response writing bypassing httputil.WriteJSONResponse in proxy/handler.go #3312

@github-actions

Description

@github-actions

Part of duplicate code analysis: #3309

Summary

internal/proxy/handler.go contains two locations where JSON HTTP responses are written by manually setting the Content-Type header, calling w.WriteHeader, and then w.Write. The project already provides httputil.WriteJSONResponse (in internal/httputil/httputil.go) that encapsulates exactly this 3-line pattern, but these two sites bypass it.

Duplication Details

Pattern: Manual Content-Type: application/json + WriteHeader + Write instead of httputil.WriteJSONResponse

  • Severity: Low
  • Occurrences: 2 (≈6 lines)
  • Locations:
    • internal/proxy/handler.go line 328–331 (inside DIFC-filtered GraphQL response path)
    • internal/proxy/handler.go line 365–367 (inside writeEmptyResponse)
  • Code Sample:
// handler.go:328-331
copyResponseHeaders(w, resp)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
w.Write(filteredJSON)

// handler.go:364-367
func (h *proxyHandler) writeEmptyResponse(w http.ResponseWriter, resp *http.Response, originalData interface{}) {
    copyResponseHeaders(w, resp)
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(resp.StatusCode)
    ...
    w.Write([]byte(empty))
}
  • Existing helper (internal/httputil/httputil.go):
func WriteJSONResponse(w http.ResponseWriter, statusCode int, body interface{}) {
    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(statusCode)
    json.NewEncoder(w).Encode(body)
}

Note: httputil.WriteJSONResponse is already used correctly elsewhere in the same file (lines 28, 51, 83).

Impact Analysis

  • Maintainability: If the JSON response format or headers change (e.g. adding X-Content-Type-Options), the two manual sites would be missed.
  • Bug Risk: httputil.WriteJSONResponse uses json.NewEncoder(w).Encode(body) which adds a trailing newline and handles encoding; the manual sites use raw w.Write([]byte) which skips encoding safety.
  • Consistency: Inconsistent use of the helper within the same file.

Refactoring Recommendations

  1. Site 1 (handler.go line 328–331): Replace the manual 3-line block with a call to httputil.WriteJSONResponse(w, resp.StatusCode, json.RawMessage(filteredJSON)) after copyResponseHeaders. Note: copyResponseHeaders must still be called first to preserve upstream response headers.

  2. Site 2 (writeEmptyResponse): The empty-body case writes a raw string ("[]", "{}", {"data":null}), so httputil.WriteJSONResponse fits directly: httputil.WriteJSONResponse(w, resp.StatusCode, json.RawMessage(empty)).

    Estimated effort: 30 minutes. Purely mechanical substitution with no behavioural change.

Implementation Checklist

  • Replace manual JSON response writes at handler.go:328–331 and handler.go:364–383
  • Verify copyResponseHeaders is still called before the helper (header order matters)
  • Run make test to confirm proxy handler tests pass
  • Run make agent-finished

Parent Issue

See parent analysis report: #3309
Related to #3309

Generated by Duplicate Code Detector · ● 2.2M ·

  • expires on Apr 14, 2026, 6:05 AM UTC

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions