diff --git a/seaweed-block/core/frontend/chunkstore/layout/head.go b/seaweed-block/core/frontend/chunkstore/layout/head.go index 08ed17133d..9c896a8023 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head.go @@ -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 diff --git a/seaweed-block/core/frontend/chunkstore/layout/head_version_test.go b/seaweed-block/core/frontend/chunkstore/layout/head_version_test.go index c81cee655d..f20a41ccca 100644 --- a/seaweed-block/core/frontend/chunkstore/layout/head_version_test.go +++ b/seaweed-block/core/frontend/chunkstore/layout/head_version_test.go @@ -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) + } + } +}