layout: omit an empty ext from Head's own JSON

Head's Go JSON (the status and CLI show output) wrote a nil Ext as null,
which reads back as the bytes null, and the ext check then refused the
HEAD as a v1.0 record carrying ext. Ext is now omitted when empty, so a
HEAD without ext reads back without one; a v1.1 HEAD keeps its ext.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
pingqiuandClaude Opus 5.5 committed 2026-12-01 00:06:55 -08:00
1 parent 6eb191c38f
commit 8bdbc05ba9
2 files changed
+31 -1

No files matched your search

@@ -169,7 +169,9 @@ type Head struct {
// back verbatim (the ext contract: only additive facts that a writer
// keeping them verbatim while it changes the known fields cannot make
// wrong; anything an older writer could break bumps Version instead).
Ext json.RawMessage
// Omitted when empty in Head's own JSON (status and CLI output), so a
// HEAD without ext reads back without one, never as the bytes null.
Ext json.RawMessage `json:",omitempty"`
}
// HeadVersion1 is the v1.1 HEAD format; MaxHeadVersion the newest this
@@ -148,3 +148,31 @@ func TestHeadExtIsACanonicalObject(t *testing.T) {
}
}
}
// Head's own JSON (status and CLI output, not the HEAD codec) round-trips
// to a HEAD that still encodes: a v1.0 HEAD has no ext there, never the
// bytes null, and a v1.1 HEAD keeps its ext.
func TestHeadJSONRoundTripKeepsTheExt(t *testing.T) {
root := RootName{Hash: Sum([]byte("image"))}
v1 := idleHead()
v1.Current, v1.Position = &root, 9
v1.Version, v1.CurrentBytes, v1.Ext = HeadVersion1, u64(10), json.RawMessage(`{"a":1}`)
for name, h := range map[string]Head{"v1.0": idleHead(), "v1.1": v1} {
raw, err := json.Marshal(h)
if err != nil {
t.Fatal(err)
}
var back Head
if err := json.Unmarshal(raw, &back); err != nil {
t.Fatal(err)
}
want, err := h.Encode()
if err != nil {
t.Fatal(err)
}
got, err := back.Encode()
if err != nil || !bytes.Equal(got, want) {
t.Fatalf("%s: Head JSON %s encodes to %s, %v; want %s", name, raw, got, err, want)
}
}
}