Fix API returning null instead of empty array when no records found#14
Merged
Fix API returning null instead of empty array when no records found#14
Conversation
- 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
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>
Contributor
Author
warkanum
approved these changes
Dec 30, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix: API returning null with 200 code when no records found ✅ COMPLETE & REVIEWED
Problem Statement
The API was returning
nullor empty object{}with HTTP 206 Partial Content when no records were found for a query without an ID.Solution Implemented
✅ Returns empty array
[]instead ofnullwhen no ID provided and no records found✅ Adds
X-No-Data-Found: trueheader to indicate no data was found✅ Returns HTTP 200 OK status code (not 206 Partial Content)
All Tasks Complete
X-No-Data-Foundheader when no records foundTechnical Implementation
1.
sendFormattedResponse(handler.go:2210-2243)2.
normalizeResultArray(handler.go:2176-2208)3.
sendResponseWithOptions(handler.go:2143-2173)// Same pattern: nil → [], calculate length, set header, return 200 OK4. Tests (empty_result_test.go)
[]API Behavior
BEFORE:
AFTER:
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
pkg/restheadspec/handler.go- 3 functions updated, comments added, formatting fixedpkg/restheadspec/empty_result_test.go- new test file with 3 tests, formatting fixedOriginal prompt
💡 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.