From 333fe158e974efeb79e9f031a6a5a036537e22ee Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 30 Dec 2025 14:01:20 +0000 Subject: [PATCH] Address code review feedback - improve data length calculation clarity - Simplified data length calculation logic in sendFormattedResponse - Simplified data length calculation logic in sendResponseWithOptions - Calculate dataLen after nil conversion for clarity and consistency - All tests still passing Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com> --- pkg/restheadspec/handler.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pkg/restheadspec/handler.go b/pkg/restheadspec/handler.go index 092c70a..8f7f895 100644 --- a/pkg/restheadspec/handler.go +++ b/pkg/restheadspec/handler.go @@ -2144,15 +2144,14 @@ func (h *Handler) sendResponse(w common.ResponseWriter, data interface{}, metada func (h *Handler) sendResponseWithOptions(w common.ResponseWriter, data interface{}, metadata *common.Metadata, options *ExtendedRequestOptions) { w.SetHeader("Content-Type", "application/json") - // Calculate data length - dataLen := 0 + // Handle nil data - convert to empty array if data == nil { - // When data is nil, return empty array instead of null data = []interface{}{} - } else { - dataLen = reflection.Len(data) } + // Calculate data length after nil conversion + dataLen := reflection.Len(data) + // Add X-No-Data-Found header when no records were found if dataLen == 0 { w.SetHeader("X-No-Data-Found", "true") @@ -2210,13 +2209,14 @@ func (h *Handler) normalizeResultArray(data interface{}) interface{} { func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{}, metadata *common.Metadata, options ExtendedRequestOptions) { // Normalize single-record arrays to objects if requested httpStatus := http.StatusOK - dataLen := 0 + + // Handle nil data - convert to empty array if data == nil { - // When data is nil, return empty array instead of null data = []interface{}{} - } else { - dataLen = reflection.Len(data) } + + // Calculate data length after nil conversion + dataLen := reflection.Len(data) // Add X-No-Data-Found header when no records were found if dataLen == 0 {