Skip to content

Commit

Permalink
fix: fix ExtraFields serialization / deserialization (#193)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] authored Jun 21, 2024
1 parent 4c77c8d commit 66e02d8
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 60 deletions.
2 changes: 1 addition & 1 deletion internal/apijson/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ func (d *decoderBuilder) newStructTypeDecoder(t reflect.Type) decoderFunc {
state.exactness = extras
}

if metadata := getSubField(value, []int{-1}, "Extras"); metadata.IsValid() && len(untypedExtraFields) > 0 {
if metadata := getSubField(value, []int{-1}, "ExtraFields"); metadata.IsValid() && len(untypedExtraFields) > 0 {
metadata.Set(reflect.ValueOf(untypedExtraFields))
}
return nil
Expand Down
42 changes: 21 additions & 21 deletions internal/apijson/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ type DateTime struct {
}

type AdditionalProperties struct {
A bool `json:"a"`
Extras map[string]interface{} `json:"-,extras"`
A bool `json:"a"`
ExtraFields map[string]interface{} `json:"-,extras"`
}

type TypedAdditionalProperties struct {
A bool `json:"a"`
Extras map[string]int `json:"-,extras"`
A bool `json:"a"`
ExtraFields map[string]int `json:"-,extras"`
}

type EmbeddedStructs struct {
AdditionalProperties
A *int `json:"number2"`
Extras map[string]interface{} `json:"-,extras"`
A *int `json:"number2"`
ExtraFields map[string]interface{} `json:"-,extras"`
}

type Recursive struct {
Expand All @@ -60,21 +60,21 @@ type Recursive struct {
}

type JSONFieldStruct struct {
A bool `json:"a"`
B int64 `json:"b"`
C string `json:"c"`
D string `json:"d"`
Extras map[string]int64 `json:"-,extras"`
JSON JSONFieldStructJSON `json:"-,metadata"`
A bool `json:"a"`
B int64 `json:"b"`
C string `json:"c"`
D string `json:"d"`
ExtraFields map[string]int64 `json:"-,extras"`
JSON JSONFieldStructJSON `json:"-,metadata"`
}

type JSONFieldStructJSON struct {
A Field
B Field
C Field
D Field
Extras map[string]Field
raw string
A Field
B Field
C Field
D Field
ExtraFields map[string]Field
raw string
}

type UnknownStruct struct {
Expand Down Expand Up @@ -325,7 +325,7 @@ var tests = map[string]struct {
`{"a":true,"bar":"value","foo":true}`,
AdditionalProperties{
A: true,
Extras: map[string]interface{}{
ExtraFields: map[string]interface{}{
"bar": "value",
"foo": true,
},
Expand All @@ -349,7 +349,7 @@ var tests = map[string]struct {
B: Field{raw: `"12"`, status: valid},
C: Field{raw: "null", status: null},
D: Field{raw: "", status: missing},
Extras: map[string]Field{
ExtraFields: map[string]Field{
"extra_typed": {
raw: "12",
status: valid,
Expand All @@ -360,7 +360,7 @@ var tests = map[string]struct {
},
},
},
Extras: map[string]int64{
ExtraFields: map[string]int64{
"extra_typed": 12,
"extra_untyped": 0,
},
Expand Down
8 changes: 4 additions & 4 deletions internal/apijson/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,15 @@ func Port(from any, to any) error {
}
}

// Finally, copy over the .JSON.raw and .JSON.Extras
// Finally, copy over the .JSON.raw and .JSON.ExtraFields
if toJSON.IsValid() {
if raw := toJSON.FieldByName("raw"); raw.IsValid() {
setUnexportedField(raw, fromJSON.Interface().(interface{ RawJSON() string }).RawJSON())
}

if toExtras := toJSON.FieldByName("Extras"); toExtras.IsValid() {
if fromExtras := fromJSON.FieldByName("Extras"); fromExtras.IsValid() {
setUnexportedField(toExtras, fromExtras.Interface())
if toExtraFields := toJSON.FieldByName("ExtraFields"); toExtraFields.IsValid() {
if fromExtraFields := fromJSON.FieldByName("ExtraFields"); fromExtraFields.IsValid() {
setUnexportedField(toExtraFields, fromExtraFields.Interface())
}
}
}
Expand Down
68 changes: 34 additions & 34 deletions internal/apijson/port_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ type Card struct {
}

type cardJSON struct {
Processor Field
Data Field
IsFoo Field
IsBar Field
Metadata Field
Value Field
Extras map[string]Field
raw string
Processor Field
Data Field
IsFoo Field
IsBar Field
Metadata Field
Value Field
ExtraFields map[string]Field
raw string
}

func (r cardJSON) RawJSON() string { return r.raw }
Expand All @@ -48,13 +48,13 @@ type CardVisa struct {
}

type cardVisaJSON struct {
Processor Field
Data Field
IsFoo Field
Metadata Field
Value Field
Extras map[string]Field
raw string
Processor Field
Data Field
IsFoo Field
Metadata Field
Value Field
ExtraFields map[string]Field
raw string
}

func (r cardVisaJSON) RawJSON() string { return r.raw }
Expand All @@ -77,13 +77,13 @@ type CardMastercard struct {
}

type cardMastercardJSON struct {
Processor Field
Data Field
IsBar Field
Metadata Field
Value Field
Extras map[string]Field
raw string
Processor Field
Data Field
IsBar Field
Metadata Field
Value Field
ExtraFields map[string]Field
raw string
}

func (r cardMastercardJSON) RawJSON() string { return r.raw }
Expand All @@ -110,12 +110,12 @@ var portTests = map[string]struct {
},
Value: "value",
JSON: cardVisaJSON{
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"foo"}}`,
Processor: Field{raw: `"visa"`, status: valid},
IsFoo: Field{raw: `true`, status: valid},
Data: Field{raw: `{"foo":"foo"}`, status: valid},
Value: Field{raw: `"value"`, status: valid},
Extras: map[string]Field{"extra": {raw: `"yo"`, status: valid}},
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"foo"}}`,
Processor: Field{raw: `"visa"`, status: valid},
IsFoo: Field{raw: `true`, status: valid},
Data: Field{raw: `{"foo":"foo"}`, status: valid},
Value: Field{raw: `"value"`, status: valid},
ExtraFields: map[string]Field{"extra": {raw: `"yo"`, status: valid}},
},
},
Card{
Expand All @@ -130,12 +130,12 @@ var portTests = map[string]struct {
},
Value: "value",
JSON: cardJSON{
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"foo"}}`,
Processor: Field{raw: `"visa"`, status: valid},
IsFoo: Field{raw: `true`, status: valid},
Data: Field{raw: `{"foo":"foo"}`, status: valid},
Value: Field{raw: `"value"`, status: valid},
Extras: map[string]Field{"extra": {raw: `"yo"`, status: valid}},
raw: `{"processor":"visa","is_foo":true,"data":{"foo":"foo"}}`,
Processor: Field{raw: `"visa"`, status: valid},
IsFoo: Field{raw: `true`, status: valid},
Data: Field{raw: `{"foo":"foo"}`, status: valid},
Value: Field{raw: `"value"`, status: valid},
ExtraFields: map[string]Field{"extra": {raw: `"yo"`, status: valid}},
},
},
},
Expand Down

0 comments on commit 66e02d8

Please sign in to comment.