Skip to content

[duplicate-code] Duplicate Code Pattern: Inline JSON Response Construction in proxy/handler.go #2301

@github-actions

Description

@github-actions

Part of duplicate code analysis: #2300

Summary

The same 3-line JSON response construction block appears 4 times in internal/proxy/handler.go. A centralised writeJSONResponse helper already exists in internal/server/http_helpers.go but is not accessible to the proxy package. The proxy package duplicates this logic inline instead.

Duplication Details

Pattern: Inline JSON response headers + body encoding

  • Severity: Medium

  • Occurrences: 4 instances in the same file

  • Locations:

    • internal/proxy/handler.go (lines 35–37) — health-check response
    • internal/proxy/handler.go (lines 69–71) — unknown GraphQL operation forbidden
    • internal/proxy/handler.go (lines 135–137) — write operation DIFC block
    • internal/proxy/handler.go (lines 214–216) — strict-mode collection block
  • Existing helper (not used by proxy):

    • internal/server/http_helpers.go (lines 22–26) — writeJSONResponse(w, statusCode, body)
  • Code Sample (each occurrence looks like this):

    w.Header().Set("Content-Type", "application/json")
    w.WriteHeader(http.StatusForbidden)
    json.NewEncoder(w).Encode(map[string]string{
        "message": "...",
    })

Impact Analysis

  • Maintainability: Every future change to the response format (e.g., adding a requestId field, switching to json.Marshal + explicit w.Write) must be applied in 4 places instead of 1.
  • Bug Risk: Content-Type header could be omitted or set to the wrong value in a future edit to any of the 4 sites.
  • Code Bloat: ~12 lines that could be 4 single-line calls.

Refactoring Recommendations

  1. Add a package-level helper to internal/proxy/

    • Create internal/proxy/http_helpers.go containing:
      func writeJSONResponse(w http.ResponseWriter, status int, body interface{}) {
          w.Header().Set("Content-Type", "application/json")
          w.WriteHeader(status)
          json.NewEncoder(w).Encode(body)
      }
    • Replace all 4 inline blocks in handler.go with calls to this helper.
    • Estimated effort: ~30 minutes
    • Benefits: single place to adjust response serialisation; consistent behaviour
  2. Alternative: Extract to a shared internal/httputil package

    • If server/http_helpers.go's writeJSONResponse is needed by more packages in the future, move it to internal/httputil/ and import from both server and proxy.
    • More disruptive but more scalable.

Implementation Checklist

  • Review the 4 duplication sites in internal/proxy/handler.go
  • Create writeJSONResponse helper in proxy package (or shared package)
  • Replace 4 inline blocks with helper calls
  • Verify tests still pass (make test)
  • Confirm no functionality broken

Parent Issue

See parent analysis report: #2300
Related to #2300

Generated by Duplicate Code Detector ·

  • expires on Mar 29, 2026, 3:03 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