Skip to content

fix(display): report files as [] instead of null when a pull produces none - #2175

Merged
TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:fix/pull-files-empty-array
Sep 21, 2026
Merged

TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:fix/pull-files-empty-array

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

Refs #2169 — the pull half.

A note on that issue: it tracked both halves, but it auto-closed when #2170 merged because I used a closing keyword there while deliberately fixing only push. That was my mistake; happy to reopen it if you would rather track the remaining half there.

Problem

model.NewPull passes the caller's slice straight into files, and Pulled.Files() returns slices.Clone of a nil slice — which is also nil. So a pull that produced no files marshals as:

{ "files": null }

and a go-template taking its length fails outright:

error calling len: len of nil pointer

OnFilePulled only fires for layers carrying org.opencontainers.image.title, so an artifact with no titled layer reaches this path.

This is the same inconsistency fixed for push in #2170: repositories, tags and referrers all report [] when empty, so the same template shape worked for every command except pull.

Fix

 func NewPull(digestReference string, files []File) any {
+	if files == nil {
+		files = []File{}
+	}

NewPull is the single serialisation boundary — both the JSON and template handlers call it with ph.pulled.Files() — so one guard covers both.

Tests

Written first and confirmed failing on main:

Test On main With fix
model TestNewPull_files (nil / empty / one file) files = null, want [] pass
json TestPullHandler_Render_noFiles files = null, want [] pass
template TestPullHandler_Render_noFilesLen ({{len .files}}) len of nil pointer pass

The model test compares the raw JSON value, so it distinguishes null from [] rather than accepting either.

Compatibility

For a pull with no files, --format json changes files from null to []. Nothing depended on null: the template TestPullHandler_Render_noFiles added in #2168 uses {{range .files}}, which tolerates either form and still passes, and the e2e suite only ranges over the field.

go test ./..., gofmt and go vet are clean.

… none

model.NewPull passes the caller's slice straight into the `files` field, and
Pulled.Files() returns slices.Clone of a nil slice, which is also nil. A pull
that produced no files therefore marshalled the field as null, and a
go-template taking its length failed:

    error calling len: len of nil pointer

OnFilePulled only fires for layers carrying org.opencontainers.image.title,
so an artifact with no titled layer reaches this path.

This is the pull half of the same inconsistency fixed for push in oras-project#2170: the
repositories, tags and referrers lists all report [] when empty, so the same
template shape worked for every command except pull.

Initialise the slice to []File{} when nil, matching the style used for
referenceAsTags.

Add regression tests at each layer the value passes through, each confirmed
to fail before this change:

  - model: NewPull marshals files as [] for nil and empty input, comparing
    the raw JSON so null and [] are told apart
  - json handler: a pull with no files reports files as []
  - template handler: {{len .files}} renders 0 instead of erroring

The existing template TestPullHandler_Render_noFiles uses {{range .files}},
which tolerates either form, and still passes.

Signed-off-by: Akshita <[email protected]>
@codecov

codecov Bot commented Sep 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.82%. Comparing base (87495d5) to head (748977b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2175   +/-   ##
=======================================
  Coverage   87.81%   87.82%           
=======================================
  Files         139      139           
  Lines        5689     5691    +2     
=======================================
+ Hits         4996     4998    +2     
  Misses        413      413           
  Partials      280      280           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

/lgtm

@TerryHowe
TerryHowe merged commit ce4dc19 into oras-project:main Sep 21, 2026
9 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.

2 participants