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 {