From 66e02d8159cd820f8300997b633d5a340e2cbb73 Mon Sep 17 00:00:00 2001 From: "stainless-app[bot]" <142633134+stainless-app[bot]@users.noreply.github.com> Date: Fri, 21 Jun 2024 20:26:53 +0000 Subject: [PATCH] fix: fix ExtraFields serialization / deserialization (#193) --- internal/apijson/decoder.go | 2 +- internal/apijson/json_test.go | 42 +++++++++++----------- internal/apijson/port.go | 8 ++--- internal/apijson/port_test.go | 68 +++++++++++++++++------------------ 4 files changed, 60 insertions(+), 60 deletions(-) diff --git a/internal/apijson/decoder.go b/internal/apijson/decoder.go index f3c4f19..deb0bac 100644 --- a/internal/apijson/decoder.go +++ b/internal/apijson/decoder.go @@ -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 diff --git a/internal/apijson/json_test.go b/internal/apijson/json_test.go index 81cea3b..5cdc456 100644 --- a/internal/apijson/json_test.go +++ b/internal/apijson/json_test.go @@ -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 { @@ -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 { @@ -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, }, @@ -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, @@ -360,7 +360,7 @@ var tests = map[string]struct { }, }, }, - Extras: map[string]int64{ + ExtraFields: map[string]int64{ "extra_typed": 12, "extra_untyped": 0, }, diff --git a/internal/apijson/port.go b/internal/apijson/port.go index a7d680e..80b323b 100644 --- a/internal/apijson/port.go +++ b/internal/apijson/port.go @@ -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()) } } } diff --git a/internal/apijson/port_test.go b/internal/apijson/port_test.go index a49213a..f9b6e3f 100644 --- a/internal/apijson/port_test.go +++ b/internal/apijson/port_test.go @@ -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 } @@ -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 } @@ -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 } @@ -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{ @@ -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}}, }, }, },