Skip to content

fix(api): serialize empty read results as [] instead of null - #1449

Open
YO-WHATS-UP2 wants to merge 1 commit into
flatcar:mainfrom
YO-WHATS-UP2:fix/empty-array-json-serialization
Open

fix(api): serialize empty read results as [] instead of null#1449
YO-WHATS-UP2 wants to merge 1 commit into
flatcar:mainfrom
YO-WHATS-UP2:fix/empty-array-json-serialization

Conversation

@YO-WHATS-UP2

@YO-WHATS-UP2 YO-WHATS-UP2 commented Jul 30, 2026

Copy link
Copy Markdown

fix(api): serialize empty read results as [] instead of null

Fixes #588

dbreads declared its result slices with var x []T, so a query returning zero rows
yielded a nil slice — which encoding/json marshals as null. spec.yaml declares
these fields type: array and marks none of them nullable, so the API contradicted
its 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 = nil assignments, so "extra_files": null was emitted
for every package without extra files — the common case.

Also drops the workaround in GetGroupVersionBreakdown, which returned []string{}
where the schema expects versionBreakdownEntry objects, and only worked because empty
arrays serialize identically regardless of element type.

Testing done

The new test asserts the marshalled bytes equal [] rather than using assert.Empty,
which passes for a nil slice too. Confirmed failing on the previous code.

  • Changelog entries added in the respective changelog/ directory (user-facing change, bug fix, security fix, update)
  • Inspected CI output for image differences: /boot and /usr size, packages, list files for any missing binaries, kernel modules, config files, kernel modules, etc.

Copilot AI lite review requested due to automatic review settings July 30, 2026 14:34
@YO-WHATS-UP2
YO-WHATS-UP2 marked this pull request as ready for review July 30, 2026 14:34
@YO-WHATS-UP2
YO-WHATS-UP2 requested a review from a team as a code owner July 30, 2026 14:34

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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{}) in dbreads query methods so zero-row queries marshal as [].
  • Ensure package extra_files is emitted as [] (including the common “no extra files” case) by initializing/assigning empty slices rather than nil.
  • 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.

@YO-WHATS-UP2
YO-WHATS-UP2 force-pushed the fix/empty-array-json-serialization branch from cd72502 to 3d5b1b4 Compare July 30, 2026 14:38
Copilot AI review requested due to automatic review settings July 30, 2026 14:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

@YO-WHATS-UP2
YO-WHATS-UP2 force-pushed the fix/empty-array-json-serialization branch 2 times, most recently from 93b3e49 to 73e8fc1 Compare August 1, 2026 15:07
Copilot AI review requested due to automatic review settings August 1, 2026 15:07

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

@ervcz

ervcz commented Aug 10, 2026

Copy link
Copy Markdown
Collaborator

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.

Copilot AI review requested due to automatic review settings August 10, 2026 15:33
@YO-WHATS-UP2
YO-WHATS-UP2 force-pushed the fix/empty-array-json-serialization branch from 73e8fc1 to f20f478 Compare August 10, 2026 15:33

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

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>
@YO-WHATS-UP2
YO-WHATS-UP2 force-pushed the fix/empty-array-json-serialization branch from f20f478 to e4716fa Compare August 22, 2026 11:49
Copilot AI review requested due to automatic review settings August 22, 2026 11:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated no new comments.

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.

backend API null returned for empty []

3 participants