Skip to content

Commit 02a2004

Browse files
fix(encoder): correctly serialize NullStruct
1 parent 36175b7 commit 02a2004

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

packages/param/encoder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func MarshalUnion[T ParamStruct](metadata T, variants ...any) ([]byte, error) {
8383
}
8484
}
8585
if nPresent == 0 || presentIdx == -1 {
86+
if metadata.null() {
87+
return []byte("null"), nil
88+
}
8689
if ovr, ok := metadata.Overrides(); ok {
8790
return shimjson.Marshal(ovr)
8891
}

packages/param/encoder_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,15 @@ func TestOverriddenUnion(t *testing.T) {
363363
})
364364
}
365365
}
366+
367+
func TestNullStructUnion(t *testing.T) {
368+
nullUnion := param.NullStruct[PrimitiveUnion]()
369+
370+
b, err := json.Marshal(nullUnion)
371+
if err != nil {
372+
t.Fatalf("didn't expect error %v", err)
373+
}
374+
if string(b) != "null" {
375+
t.Fatalf("expected null, received %s", string(b))
376+
}
377+
}

0 commit comments

Comments
 (0)