fix(encoder): correctly serialize NullStruct

This commit is contained in:
stainless-app[bot]
2026-02-10 20:31:14 +00:00
parent 6c11b38c75
commit fa21441332
2 changed files with 15 additions and 0 deletions

View File

@@ -83,6 +83,9 @@ func MarshalUnion[T ParamStruct](metadata T, variants ...any) ([]byte, error) {
}
}
if nPresent == 0 || presentIdx == -1 {
if metadata.null() {
return []byte("null"), nil
}
if ovr, ok := metadata.Overrides(); ok {
return shimjson.Marshal(ovr)
}

View File

@@ -363,3 +363,15 @@ func TestOverriddenUnion(t *testing.T) {
})
}
}
func TestNullStructUnion(t *testing.T) {
nullUnion := param.NullStruct[PrimitiveUnion]()
b, err := json.Marshal(nullUnion)
if err != nil {
t.Fatalf("didn't expect error %v", err)
}
if string(b) != "null" {
t.Fatalf("expected null, received %s", string(b))
}
}