fix(api): serialize empty read results as [] instead of null - #1449
fix(api): serialize empty read results as [] instead of null#1449YO-WHATS-UP2 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes JSON serialization of empty “list” results in the backend API to consistently emit [] (empty array) rather than null, bringing responses in line with the OpenAPI schema (non-nullable arrays) and removing a handler-level workaround that returned the wrong element type.
Changes:
- Initialize result slices to non-nil empty slices (
[]T{}) indbreadsquery methods so zero-row queries marshal as[]. - Ensure package
extra_filesis emitted as[](including the common “no extra files” case) by initializing/assigning empty slices rather thannil. - Add a regression test that asserts marshalled JSON is exactly
[], and update an existing activity test to assert non-nil empty results.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| backend/pkg/handler/packages.go | Ensures ExtraFiles is always a non-nil slice when building package objects for JSON responses. |
| backend/pkg/handler/groups.go | Removes the incorrect-type workaround for empty version breakdown responses; relies on proper empty-slice behavior instead. |
| backend/pkg/api/internal/dbreads/packages.go | Returns non-nil empty slices for empty package reads and ensures ExtraFiles is set to [] when absent. |
| backend/pkg/api/internal/dbreads/packages_floors.go | Returns a non-nil empty slice when a package has no floor channels. |
| backend/pkg/api/internal/dbreads/instances.go | Returns a non-nil empty slice for empty instance status history and instance list results. |
| backend/pkg/api/internal/dbreads/groups.go | Returns a non-nil empty slice for empty group lists and group version breakdown results. |
| backend/pkg/api/internal/dbreads/channels.go | Returns a non-nil empty slice for empty channel lists. |
| backend/pkg/api/internal/dbreads/applications.go | Returns a non-nil empty slice for empty application lists. |
| backend/pkg/api/internal/dbreads/activity.go | Returns a non-nil empty slice for empty activity results. |
| backend/pkg/api/empty_slice_json_test.go | Adds regression coverage asserting JSON marshals to [] (not null) for several empty-result endpoints/fields. |
| backend/pkg/api/activity_test.go | Updates expectations for non-existent teams to return an empty (but non-nil) slice. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cd72502 to
3d5b1b4
Compare
93b3e49 to
73e8fc1
Compare
|
Sorry for the slow reply, and thanks for the contribution. Nebraska is one of the projects proposed for CNCF/LFX mentorship this term, and since that went up we've had a lot of attention here at once. The queue grew much faster than we can review it, so replies are slower than usual. Your PR hasn't been forgotten and it will get a proper look. Thanks for your patience. |
73e8fc1 to
f20f478
Compare
The read layer declared its result slices with `var x []T`, which marshal to `null` when no rows match. The OpenAPI spec declares these fields as `type: array` and marks none of them nullable, so the API contradicted its own contract and callers compensated with `|| []`. Refs flatcar#588 Signed-off-by: Sairam Bisoyi <sairamb2007.21@gmail.com>
f20f478 to
e4716fa
Compare
fix(api): serialize empty read results as [] instead of null
Fixes #588
dbreadsdeclared its result slices withvar x []T, so a query returning zero rowsyielded a nil slice — which
encoding/jsonmarshals asnull.spec.yamldeclaresthese fields
type: arrayand marks none of themnullable, so the API contradictedits own contract, and callers compensated with
|| [].Initialise the slices as empty at the sites whose values reach a JSON response. Two of
them were explicit
ExtraFiles = nilassignments, so"extra_files": nullwas emittedfor every package without extra files — the common case.
Also drops the workaround in
GetGroupVersionBreakdown, which returned[]string{}where the schema expects
versionBreakdownEntryobjects, and only worked because emptyarrays serialize identically regardless of element type.
Testing done
The new test asserts the marshalled bytes equal
[]rather than usingassert.Empty,which passes for a nil slice too. Confirmed failing on the previous code.
changelog/directory (user-facing change, bug fix, security fix, update)/bootand/usrsize, packages, list files for any missing binaries, kernel modules, config files, kernel modules, etc.