From 78023594b5bb7287da01985a822dc1e4e0fac023 Mon Sep 17 00:00:00 2001 From: Denis Arh Date: Mon, 21 Dec 2020 22:47:11 +0100 Subject: [PATCH] Fix accidental casting of JSON null to int(0) --- pkg/filter/pagination.go | 7 +++++++ pkg/filter/pagination_test.go | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/pkg/filter/pagination.go b/pkg/filter/pagination.go index 1c4643929..4e4a9c203 100644 --- a/pkg/filter/pagination.go +++ b/pkg/filter/pagination.go @@ -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 } diff --git a/pkg/filter/pagination_test.go b/pkg/filter/pagination_test.go index f3d9556f3..e70dc90d5 100644 --- a/pkg/filter/pagination_test.go +++ b/pkg/filter/pagination_test.go @@ -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) + }) + } + }