diff --git a/ovh/ovh.go b/ovh/ovh.go index 873f086..9bccb20 100644 --- a/ovh/ovh.go +++ b/ovh/ovh.go @@ -417,5 +417,7 @@ func (c *Client) UnmarshalResponse(response *http.Response, resType interface{}) return nil } - return json.Unmarshal(body, &resType) + d := json.NewDecoder(bytes.NewReader(body)) + d.UseNumber() + return d.Decode(&resType) } diff --git a/ovh/ovh_test.go b/ovh/ovh_test.go index 398d3b7..c4e3ab4 100644 --- a/ovh/ovh_test.go +++ b/ovh/ovh_test.go @@ -389,6 +389,52 @@ func TestGetResponse(t *testing.T) { } } +func TestGetResponseUnmarshalNumber(t *testing.T) { + var err error + var output map[string]interface{} + mockClient := Client{} + + // with map[string]interface{} as output + err = mockClient.UnmarshalResponse(&http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"orderId": 1234567890}`)), + }, &output) + if err != nil { + t.Fatalf("Client.UnmarshalResponse should be able to decode the body") + } + if "1234567890" != fmt.Sprint(output["orderId"]) { + t.Fatalf("Client.UnmarshalResponse should unmarshal long integer as json.Number instead of float64, stringified incorrectly") + } + + var outputInt map[string]int64 + + // with map[string]int64 as output + err = mockClient.UnmarshalResponse(&http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"orderId": 1234567890}`)), + }, &outputInt) + if err != nil { + t.Fatalf("Client.UnmarshalResponse should be able to decode the body") + } + if int64(1234567890) != outputInt["orderId"] { + t.Fatalf("Client.UnmarshalResponse should unmarshal long integer as json.Number instead of float64, incorrectly casted as int64") + } + + var outputFloat map[string]float64 + + // with map[string]int64 as output + err = mockClient.UnmarshalResponse(&http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(strings.NewReader(`{"orderId": 1234567890}`)), + }, &outputFloat) + if err != nil { + t.Fatalf("Client.UnmarshalResponse should be able to decode the body") + } + if float64(1234567890) != outputFloat["orderId"] { + t.Fatalf("Client.UnmarshalResponse should unmarshal long integer as json.Number instead of float64, incorrectly casted as float64") + } +} + func TestConstructors(t *testing.T) { // Nominal: full constructor client, err := NewClient("ovh-eu", MockApplicationKey, MockApplicationSecret, MockConsumerKey)