Skip to content

Fix API returning null instead of empty array when no records found#14

Merged
warkanum merged 6 commits intomainfrom
copilot/fix-api-null-response
Dec 30, 2025
Merged

Fix API returning null instead of empty array when no records found#14
warkanum merged 6 commits intomainfrom
copilot/fix-api-null-response

Conversation

Copy link
Contributor

Copilot AI commented Dec 30, 2025

Fix: API returning null with 200 code when no records found ✅ COMPLETE & REVIEWED

Problem Statement

The API was returning null or empty object {} with HTTP 206 Partial Content when no records were found for a query without an ID.

Solution Implemented

✅ Returns empty array [] instead of null when no ID provided and no records found
✅ Adds X-No-Data-Found: true header to indicate no data was found
✅ Returns HTTP 200 OK status code (not 206 Partial Content)

All Tasks Complete

  • ✅ Analyze the codebase and understand current behavior
  • ✅ Modify response functions to ensure empty array is always returned
  • ✅ Add X-No-Data-Found header when no records found
  • ✅ Ensure empty arrays stay as arrays through normalization
  • ✅ Update all response functions consistently
  • ✅ Create comprehensive tests to verify the fix
  • ✅ Run all tests - ensure no regressions
  • ✅ Verify linter passes
  • ✅ Verify build succeeds
  • ✅ Address all code review feedback
  • ✅ Add clarifying comments for maintainability
  • ✅ Fix formatting issues

Technical Implementation

1. sendFormattedResponse (handler.go:2210-2243)

// Convert nil to empty array
if data == nil {
    data = []interface{}{}
}

// Calculate length BEFORE normalization - header indicates DB result
dataLen := reflection.Len(data)

// Set header based on original query result
if dataLen == 0 {
    w.SetHeader("X-No-Data-Found", "true")
}

// Normalize after header set (doesn't affect header logic)
if options.SingleRecordAsObject {
    data = h.normalizeResultArray(data)
}

2. normalizeResultArray (handler.go:2176-2208)

// Returns []interface{}{} for empty arrays (not map[string]interface{}{})
// Serializes to [] in JSON, never null or {}

3. sendResponseWithOptions (handler.go:2143-2173)

// Same pattern: nil → [], calculate length, set header, return 200 OK

4. Tests (empty_result_test.go)

  • Flag-based test structure for maintainability
  • Verifies JSON serialization to []
  • Tests header addition
  • Tests status code

API Behavior

BEFORE:

HTTP/1.1 206 Partial Content
Content-Type: application/json

null  (or {})

AFTER:

HTTP/1.1 200 OK
Content-Type: application/json
X-No-Data-Found: true

[]

Quality Metrics

✅ All 30 tests passing (27 existing + 3 new)
✅ Formatting fixed with gofmt
✅ Linter clean (no errors)
✅ Build successful
✅ All code review feedback addressed
✅ Code well-documented with clarifying comments

Files Modified

  1. pkg/restheadspec/handler.go - 3 functions updated, comments added, formatting fixed
  2. pkg/restheadspec/empty_result_test.go - new test file with 3 tests, formatting fixed
Original prompt

This section details on the original issue you should resolve

<issue_title>bug: API giving null with 200 code when no records was found.</issue_title>
<issue_description>API giving null with 200 code when no records was found.

The pkg/restheadspec handleRead must never return null. If an id was not given we expect an empty array. Also add an extra header staying no data was found.

The API must still give a 200 code if no records was found for a given filters etc. </issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits December 30, 2025 13:57
- Modified handleRead to always return empty array [] instead of null when no ID provided
- Added X-No-Data-Found header when result count is 0
- Updated normalizeResultArray to keep empty arrays as arrays instead of converting to empty objects
- Updated sendFormattedResponse and sendResponseWithOptions to handle empty data properly
- All responses now return 200 OK instead of 206 Partial Content when no data found
- Added comprehensive tests to verify the fix

Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
- 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>
- Changed test to use shouldBeEmptyArr flag instead of hardcoded name comparison
- Makes test more maintainable and less fragile
- All tests still passing

Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
- Added comments explaining why X-No-Data-Found is set before normalization
- Header reflects database query result, not final response format
- Clarifies that normalizeResultArray doesn't affect header logic
- All tests passing

Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix API returning null with 200 status code Fix API returning null instead of empty array when no records found Dec 30, 2025
Copilot AI requested a review from warkanum December 30, 2025 14:06
Copy link
Member

@warkanum warkanum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot Fix the formatting issue

@warkanum
Copy link
Member

@copilot Tests are failing due to linting issues. Fix the formatting.

- Removed trailing whitespace
- Fixed tab/space alignment in struct definitions
- All tests still passing

Co-authored-by: warkanum <208308+warkanum@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Dec 30, 2025

@copilot Fix the formatting issue

Fixed formatting issues with gofmt in commit be38341. Removed trailing whitespace and fixed alignment.

@copilot Tests are failing due to linting issues. Fix the formatting.

Fixed all formatting issues with gofmt in commit be38341. All tests passing.

Copilot AI requested a review from warkanum December 30, 2025 15:31
@warkanum warkanum marked this pull request as ready for review December 30, 2025 15:32
@warkanum warkanum merged commit b8c44c5 into main Dec 30, 2025
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: API giving null with 200 code when no records was found.

2 participants