@@ -662,6 +662,14 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
662662 return
663663 }
664664
665+ // Check if a specific ID was requested but no record was found
666+ resultCount := reflection .Len (modelPtr )
667+ if id != "" && resultCount == 0 {
668+ logger .Warn ("Record not found for ID: %s" , id )
669+ h .sendError (w , http .StatusNotFound , "not_found" , "Record not found" , nil )
670+ return
671+ }
672+
665673 limit := 0
666674 if options .Limit != nil {
667675 limit = * options .Limit
@@ -676,7 +684,7 @@ func (h *Handler) handleRead(ctx context.Context, w common.ResponseWriter, id st
676684
677685 metadata := & common.Metadata {
678686 Total : int64 (total ),
679- Count : int64 (reflection . Len ( modelPtr ) ),
687+ Count : int64 (resultCount ),
680688 Filtered : int64 (total ),
681689 Limit : limit ,
682690 Offset : offset ,
@@ -2116,7 +2124,7 @@ func (h *Handler) sendResponseWithOptions(w common.ResponseWriter, data interfac
21162124// Returns the single element if data is a slice/array with exactly one element, otherwise returns data unchanged
21172125func (h * Handler ) normalizeResultArray (data interface {}) interface {} {
21182126 if data == nil {
2119- return nil
2127+ return map [ string ] interface {}{}
21202128 }
21212129
21222130 // Use reflection to check if data is a slice or array
@@ -2125,10 +2133,15 @@ func (h *Handler) normalizeResultArray(data interface{}) interface{} {
21252133 dataValue = dataValue .Elem ()
21262134 }
21272135
2128- // Check if it's a slice or array with exactly one element
2129- if (dataValue .Kind () == reflect .Slice || dataValue .Kind () == reflect .Array ) && dataValue .Len () == 1 {
2130- // Return the single element
2131- return dataValue .Index (0 ).Interface ()
2136+ // Check if it's a slice or array
2137+ if dataValue .Kind () == reflect .Slice || dataValue .Kind () == reflect .Array {
2138+ if dataValue .Len () == 1 {
2139+ // Return the single element
2140+ return dataValue .Index (0 ).Interface ()
2141+ } else if dataValue .Len () == 0 {
2142+ // Return empty object instead of empty array
2143+ return map [string ]interface {}{}
2144+ }
21322145 }
21332146
21342147 return data
0 commit comments