Skip to content

Commit

Permalink
Fix uint32 unmarshal (#2631)
Browse files Browse the repository at this point in the history
The string unmarshal for uint32 used ParseInt instead of ParseUint,
which would parse the wrong range of valid numbers.

Co-authored-by: Marina Valverde <marina.valverde@ifood.com.br>
  • Loading branch information
mdvalv and Marina Valverde authored May 11, 2023
1 parent e62a027 commit 82a110c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion graphql/uint.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func MarshalUint32(i uint32) Marshaler {
func UnmarshalUint32(v interface{}) (uint32, error) {
switch v := v.(type) {
case string:
iv, err := strconv.ParseInt(v, 10, 32)
iv, err := strconv.ParseUint(v, 10, 32)
if err != nil {
return 0, err
}
Expand Down
3 changes: 3 additions & 0 deletions graphql/uint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package graphql

import (
"encoding/json"
"math"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -31,13 +32,15 @@ func mustUnmarshalUint(v interface{}) uint {
func TestUint32(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
assert.Equal(t, "123", m2s(MarshalUint32(123)))
assert.Equal(t, "4294967295", m2s(MarshalUint32(math.MaxUint32)))
})

t.Run("unmarshal", func(t *testing.T) {
assert.Equal(t, uint32(123), mustUnmarshalUint32(123))
assert.Equal(t, uint32(123), mustUnmarshalUint32(int64(123)))
assert.Equal(t, uint32(123), mustUnmarshalUint32(json.Number("123")))
assert.Equal(t, uint32(123), mustUnmarshalUint32("123"))
assert.Equal(t, uint32(4294967295), mustUnmarshalUint32("4294967295"))
})
}

Expand Down

0 comments on commit 82a110c

Please sign in to comment.