-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Description
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 responseinternal/proxy/handler.go(lines 69–71) — unknown GraphQL operation forbiddeninternal/proxy/handler.go(lines 135–137) — write operation DIFC blockinternal/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
requestIdfield, switching tojson.Marshal+ explicitw.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
-
Add a package-level helper to
internal/proxy/- Create
internal/proxy/http_helpers.gocontaining: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.gowith calls to this helper. - Estimated effort: ~30 minutes
- Benefits: single place to adjust response serialisation; consistent behaviour
- Create
-
Alternative: Extract to a shared
internal/httputilpackage- If
server/http_helpers.go'swriteJSONResponseis needed by more packages in the future, move it tointernal/httputil/and import from bothserverandproxy. - More disruptive but more scalable.
- If
Implementation Checklist
- Review the 4 duplication sites in
internal/proxy/handler.go - Create
writeJSONResponsehelper 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Type
Fields
Give feedbackNo fields configured for issues without a type.