File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -461,7 +461,7 @@ func (d *decoderBuilder) newStructTypeDecoder(t reflect.Type) decoderFunc {
461
461
}
462
462
463
463
// Handle null [param.Opt]
464
- if itemNode .Type == gjson .Null && dest .Type ().Implements (reflect .TypeOf ((* param .Optional )(nil )).Elem ()) {
464
+ if itemNode .Type == gjson .Null && dest .IsValid () && dest . Type ().Implements (reflect .TypeOf ((* param .Optional )(nil )).Elem ()) {
465
465
dest .Addr ().Interface ().(json.Unmarshaler ).UnmarshalJSON ([]byte (itemNode .Raw ))
466
466
continue
467
467
}
Original file line number Diff line number Diff line change
1
+ package apijson_test
2
+
3
+ import (
4
+ "encoding/json"
5
+ "github.com/openai/openai-go/internal/apijson"
6
+ "github.com/openai/openai-go/packages/respjson"
7
+ "testing"
8
+ )
9
+
10
+ type StructWithNullExtraField struct {
11
+ Results []string `json:"results,required"`
12
+ JSON struct {
13
+ Results respjson.Field
14
+ ExtraFields map [string ]respjson.Field
15
+ raw string
16
+ } `json:"-"`
17
+ }
18
+
19
+ func (r * StructWithNullExtraField ) UnmarshalJSON (data []byte ) error {
20
+ return apijson .UnmarshalRoot (data , r )
21
+ }
22
+
23
+ func TestDecodeWithNullExtraField (t * testing.T ) {
24
+ raw := `{"something_else":null}`
25
+ var dst * StructWithNullExtraField
26
+ err := json .Unmarshal ([]byte (raw ), & dst )
27
+ if err != nil {
28
+ t .Fatalf ("error: %s" , err .Error ())
29
+ }
30
+ }
You can’t perform that action at this time.
0 commit comments