Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow hanlding of strings that wrap a number to be deserialized to int64 #5002

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions private/protocol/json/jsonutil/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"math/big"
"reflect"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 12 additions & 0 deletions private/protocol/json/jsonutil/unmarshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down