mirror of
https://github.com/openai/openai-go.git
synced 2026-04-04 02:19:59 +09:00
fix(encoder): correctly serialize NullStruct
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user