From cbac47e052fbf7f5065099ffa52bab0fd8c46e6a Mon Sep 17 00:00:00 2001 From: Hein Date: Mon, 31 Aug 2026 16:12:59 +0200 Subject: [PATCH] fix(handler): handle JSON marshaling errors in response --- pkg/restheadspec/handler.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pkg/restheadspec/handler.go b/pkg/restheadspec/handler.go index e43a131..6ebc895 100644 --- a/pkg/restheadspec/handler.go +++ b/pkg/restheadspec/handler.go @@ -2902,8 +2902,21 @@ func (h *Handler) sendFormattedResponse(w common.ResponseWriter, data interface{ switch options.ResponseFormat { case "simple": // Simple format: just return the data array + jsonData, err := json.Marshal(data) + if err != nil { + logger.Error("Failed to marshal JSON response: %v", err) + w.WriteHeader(http.StatusInternalServerError) + return + } + if string(jsonData) == "null" { + if options.SingleRecordAsObject { + jsonData = []byte("{}") + } else { + jsonData = []byte("[]") + } + } w.WriteHeader(http.StatusOK) - if err := w.WriteJSON(data); err != nil { + if _, err := w.Write(jsonData); err != nil { logger.Error("Failed to write JSON response: %v", err) } case "syncfusion":