From aa1606d8d60f7baffe7a29ef21dcf4e45c1f5d60 Mon Sep 17 00:00:00 2001 From: LiranMs Date: Wed, 27 Sep 2023 06:36:24 +0000 Subject: [PATCH] Allow hanlding of strings that wrap a number to be deserialized to int64 Fixes issue #5000 --- private/protocol/json/jsonutil/unmarshal.go | 11 +++++++++++ private/protocol/json/jsonutil/unmarshal_test.go | 12 ++++++++++++ 2 files changed, 23 insertions(+) diff --git a/private/protocol/json/jsonutil/unmarshal.go b/private/protocol/json/jsonutil/unmarshal.go index f9334879b80..481acfa124e 100644 --- a/private/protocol/json/jsonutil/unmarshal.go +++ b/private/protocol/json/jsonutil/unmarshal.go @@ -9,6 +9,7 @@ import ( "math" "math/big" "reflect" + "strconv" "strings" "time" @@ -259,6 +260,16 @@ func (u unmarshaler) unmarshalScalar(value reflect.Value, data interface{}, tag return err } value.Set(reflect.ValueOf(v)) + case *int64: + v, err := strconv.ParseInt(d, 10, 64) + if err != nil { + fp, err := strconv.ParseFloat(d, 64) + if err != nil { + return fmt.Errorf("failed converting string that should contain a number to int64 %s: %w", d, err) + } + v = int64(fp) + } + value.Set(reflect.ValueOf(aws.Int64(v))) case *float64: // These are regular strings when parsed by encoding/json's unmarshaler. switch { diff --git a/private/protocol/json/jsonutil/unmarshal_test.go b/private/protocol/json/jsonutil/unmarshal_test.go index 1e7ec3615ef..235a230a31d 100644 --- a/private/protocol/json/jsonutil/unmarshal_test.go +++ b/private/protocol/json/jsonutil/unmarshal_test.go @@ -102,6 +102,18 @@ func TestUnmarshalJSON_JSONNumber(t *testing.T) { IntField: aws.Int64(123456789), }, }, + "integer field as string": { + JSON: `{"intField":"123456789"}`, + Expected: input{ + IntField: aws.Int64(123456789), + }, + }, + "integer field as string truncated": { + JSON: `{"intField":"123456789.123"}`, + Expected: input{ + IntField: aws.Int64(123456789), + }, + }, "float64 field": { JSON: `{"floatField":123456789.123}`, Expected: input{