Skip to content

fix(display): report referenceAsTags as [] instead of null when untagged - #2170

Merged
TerryHowe merged 2 commits into
oras-project:mainfrom
akshita317:fix/push-reference-as-tags-empty
Sep 18, 2026
Merged

TerryHowe merged 2 commits into
oras-project:mainfrom
akshita317:fix/push-reference-as-tags-empty

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

Fixes #2169 (the push half).

Problem

model.NewPush built referenceAsTags with var refAsTags []string and only appended, so an untagged push marshalled it as null:

{ "referenceAsTags": null }

and a go-template taking its length failed outright:

error calling len: len of nil pointer

repo ls (repositories), repo tags (tags) and discover (referrers) all initialise to an empty slice and report [], so the same template shape worked for those commands but not for push. Confirmed in review of #2164.

Fix

-	var refAsTags []string
+	refAsTags := make([]string, 0, len(tags))

Empty now marshals as [], and the slice is preallocated to its final length.

Tests

Regression tests at each layer the value passes through, all written first and confirmed failing on main:

Test On main With fix
model TestNewPush_referenceAsTags (nil / empty / 1 / 2 tags) nil and empty cases: referenceAsTags = null, want [] pass
json TestPushHandler_Render_untagged referenceAsTags = null, want [] pass
template TestPushHandler_Render_untaggedLen ({{len .referenceAsTags}}) len of nil pointer pass

The model test compares the raw JSON value, so it distinguishes null from [] rather than accepting either. The existing tagged-push assertions are unchanged and still pass.

Compatibility

For an untagged oras push --format json, referenceAsTags changes from null to [], matching the other list fields. Nothing in the repository depended on null: the existing JSON push test compares with slices.Equal, which treats nil and empty alike, and the e2e suite only ranges over the field.

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

Not in this PR

pull's files has the same root cause (see #2169) and lives in model/pull.go. I've kept this PR to push as discussed; happy to follow up with pull separately or add it here, whichever you prefer.

model.NewPush built referenceAsTags with `var refAsTags []string` and only
appended to it, so an untagged push marshalled the field as null. A
go-template taking its length then failed:

    error calling len: len of nil pointer

The other list fields in the metadata models -- repositories for repo ls,
tags for repo tags and referrers for discover -- are all initialised to an
empty slice and report [], so the same template shape worked for those
commands but not for push.

Initialise the slice with make([]string, 0, len(tags)) so an empty list
marshals as [], preallocating it to its final length.

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

  - model: NewPush marshals referenceAsTags as [] for nil and empty tags,
    comparing the raw JSON so null and [] are told apart
  - json handler: an untagged Render reports referenceAsTags as []
  - template handler: {{len .referenceAsTags}} renders 0 after an untagged
    push instead of erroring

For an untagged `oras push --format json` the field changes from null to [].
Nothing in the repository depended on null: the existing JSON push test
compares with slices.Equal, which treats nil and empty alike, and the e2e
suite only ranges over the field.

pull's files field has the same root cause and is tracked in the same
issue; it is left for a separate change.

Refs oras-project#2169

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

codecov Bot commented Sep 14, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.78%. Comparing base (d1c4808) to head (8d92086).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2170      +/-   ##
==========================================
+ Coverage   87.71%   87.78%   +0.07%     
==========================================
  Files         139      139              
  Lines        5689     5689              
==========================================
+ Hits         4990     4994       +4     
+ Misses        419      415       -4     
  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 87495d5 into oras-project:main Sep 18, 2026
9 checks passed
@TerryHowe

Copy link
Copy Markdown
Member

Thanks!

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.

Metadata models report null instead of [] for empty lists (push referenceAsTags, pull files)

2 participants