Skip to content

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

Closed

Description

@akshita317

Problem

Two metadata models emit null instead of [] when their list is empty. The other list-carrying models initialise to an empty slice, so the same template or JSON consumer behaves differently depending on the command.

Raised in review of #2164.

Model field Initialised as JSON when empty
discover referrers []*Node{} []
repo ls repositories []string{} []
repo tags tags []string{} []
push referenceAsTags var refAsTags []string, append-only null
pull files never initialised; Files() returns slices.Clone(nil) null

oras push with no tag

model.NewPush builds referenceAsTags with var refAsTags []string and only appends, so an untagged push marshals it as null:

{ "referenceAsTags": null }

A go-template taking its length fails outright:

$ oras push localhost:5000/repo file.txt --format go-template='{{len .referenceAsTags}}'
Error: template: format output:1:2: executing "format output" at <len .referenceAsTags>:
error calling len: len of nil pointer

The same shape works for oras repo tags --format go-template='{{len .tags}}' and oras repo ls --format go-template='{{len .repositories}}'.

oras pull with no titled layer

PullHandler.OnFilePulled is only called for layers carrying org.opencontainers.image.title (cmd/oras/root/pull.go), so pulling an artifact with no titled layer leaves Pulled.files nil, and Files() returns slices.Clone(nil), which is also nil. "files" then marshals as null.

{{range .files}} — what the e2e suite uses — tolerates either form, which is why this has gone unnoticed. {{len .files}} does not.

Proposal

Initialise both to an empty slice so empty lists are always []. For push this is a one-line change:

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

I'll open a PR for push with regression tests at the model, JSON-handler and template-handler levels. pull is the same root cause in model/pull.go; happy to fold it into the same PR or do it separately, whichever you prefer. (#2002 touches that file, but only its import block.)

Nothing in the repository asserts on null here: the JSON push test compares with slices.Equal, which treats nil and empty alike, and the e2e suite only ranges over both fields.

Activity

  1. akshita317 commented on Sep 19, 2026

    @akshita317
    ContributorAuthor

    For anyone finding this later: this issue covered two halves, and it auto-closed when #2170 merged because I used a closing keyword in that PR while it deliberately fixed only the push half (referenceAsTags).

    The remaining pull half — files marshalling as null when a pull produces no files, which also makes {{len .files}} fail — is now addressed in #2175.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions