3
0

Fix accidental casting of JSON null to int(0)

This commit is contained in:
Denis Arh 2020-12-21 22:47:11 +01:00
parent 4efcf334b0
commit 78023594b5
2 changed files with 45 additions and 0 deletions

View File

@ -271,7 +271,14 @@ func (v *pagingCursorValue) UnmarshalJSON(in []byte) (err error) {
i int64
)
if string(in) == "null" {
// if we do not do this we risk conversion to int(0)
v.v = nil
return
}
if err = json.Unmarshal(in, &u); err == nil {
// handle big integers properly
v.v = u
return
}

View File

@ -61,4 +61,42 @@ func Test_cursorValueUnmarshal(t *testing.T) {
req.NoError(pcv.UnmarshalJSON([]byte(`"foo"`)))
req.Equal(pcv.v, "foo")
req.NoError(pcv.UnmarshalJSON([]byte(`null`)))
req.Nil(pcv.v)
}
func Test_cursorUnmarshal(t *testing.T) {
var (
tt = []struct {
name string
json string
cursor *PagingCursor
}{
{
"null",
`{"K":["StageName","id"],"V":[null,210277506916442116],"D":[false,false],"R":false,"LT":false}`,
&PagingCursor{
keys: []string{"StageName", "id"},
values: []interface{}{nil, uint64(210277506916442116)},
desc: []bool{false, false},
ROrder: false,
LThen: false,
},
},
}
)
for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
var (
req = require.New(t)
cur = &PagingCursor{}
)
req.NoError(cur.UnmarshalJSON([]byte(tc.json)))
req.Equal(cur, tc.cursor)
})
}
}