Skip to content

fix(list): emit [] instead of null for empty JSON list results - #208

Merged
andrew merged 2 commits into
git-pkgs:mainfrom
Karry2019web:fix/list-json-nil-207
May 27, 2026
Merged

andrew merged 2 commits into
git-pkgs:mainfrom
Karry2019web:fix/list-json-nil-207

Conversation

@Karry2019web

Copy link
Copy Markdown
Contributor

Description

When GetDependenciesWithDB returns a nil slice (no manifests found), Go's json.Encode emits null rather than [].

Downstream consumers that wrap the output (e.g. printf '{"dependencies": '; git-pkgs list --format json; printf '}') end up with {"dependencies": null}, which fails schema validation against "type": "array".

Fix

Add a nil check before encoding — coalesce to an empty slice so json.Encode produces [].

Fixes #207

When GetDependenciesWithDB returns a nil slice, json.Encode emits `null`.
Downstream consumers expecting a JSON array fail schema validation.
Coalesce to empty slice before encoding.

Fixes git-pkgs#207.

@andrew andrew 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.

Can we get a test for this as well please

@Karry2019web

Copy link
Copy Markdown
Contributor Author

I've added unit tests for outputListJSON covering nil slice, empty slice, and non-empty slice cases. See the new file cmd/list_test.go.

Copilot AI left a comment

Copy link
Copy Markdown

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 output for the list command so empty dependency results are emitted as [] instead of null, matching downstream schema expectations for array output.

Changes:

  • Coalesces nil dependency slices to an empty slice before JSON encoding.
  • Adds tests covering nil, empty, and non-empty dependency JSON output.

Reviewed changes

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

File Description
cmd/list.go Ensures outputListJSON encodes nil dependency slices as empty JSON arrays.
cmd/list_test.go Adds unit coverage for JSON serialization behavior of list output.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@andrew

andrew commented May 27, 2026

Copy link
Copy Markdown
Contributor

Thanks!

@andrew
andrew merged commit ef45ca2 into git-pkgs:main May 27, 2026
4 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.

list --format json: emit [] instead of null for empty results

3 participants