diff --git a/Makefile b/Makefile index 671b3b1b..1012cf75 100644 --- a/Makefile +++ b/Makefile @@ -16,7 +16,7 @@ $(foreach dep, $(GO_DEPENDENCIES), $(eval $(call make-go-dependency, $(dep)))) $(call make-lint-dependency) .bin/ory: Makefile - curl https://raw.githubusercontent.com/ory/meta/master/install.sh | bash -s -- -b .bin ory v0.1.48 + curl https://raw.githubusercontent.com/ory/meta/master/install.sh | bash -s -- -b .bin ory v0.2.2 touch .bin/ory .PHONY: format diff --git a/pagination/keysetpagination/header.go b/pagination/keysetpagination/header.go index e523859b..93dd43d8 100644 --- a/pagination/keysetpagination/header.go +++ b/pagination/keysetpagination/header.go @@ -11,6 +11,8 @@ import ( "strings" "github.com/pkg/errors" + + "github.com/ory/x/stringsx" ) // Pagination Request Parameters @@ -94,8 +96,8 @@ func Header(w http.ResponseWriter, u *url.URL, p *Paginator) { // Parse returns the pagination options from the URL query. func Parse(q url.Values, p PageTokenConstructor) ([]Option, error) { var opts []Option - if q.Has("page_token") { - pageToken, err := url.QueryUnescape(q.Get("page_token")) + if pt := stringsx.Coalesce(q["page_token"]...); pt != "" { + pageToken, err := url.QueryUnescape(pt) if err != nil { return nil, errors.WithStack(err) } @@ -105,8 +107,8 @@ func Parse(q url.Values, p PageTokenConstructor) ([]Option, error) { } opts = append(opts, WithToken(parsed)) } - if q.Get("page_size") != "" { - size, err := strconv.Atoi(q.Get("page_size")) + if ps := stringsx.Coalesce(q["page_size"]...); ps != "" { + size, err := strconv.Atoi(ps) if err != nil { return nil, errors.WithStack(err) } diff --git a/pagination/keysetpagination/paginator_test.go b/pagination/keysetpagination/paginator_test.go index bbc5d1af..edcff86e 100644 --- a/pagination/keysetpagination/paginator_test.go +++ b/pagination/keysetpagination/paginator_test.go @@ -168,6 +168,26 @@ func TestParse(t *testing.T) { _, err := Parse(url.Values{"page_size": {"invalid-int"}}, NewStringPageToken) require.ErrorIs(t, err, strconv.ErrSyntax) }) + + t.Run("empty tokens and page sizes work as if unset, empty values are skipped", func(t *testing.T) { + opts, err := Parse(url.Values{}, NewStringPageToken) + require.NoError(t, err) + paginator := GetPaginator(append(opts, WithDefaultToken(StringPageToken("default")))...) + assert.Equal(t, "default", paginator.Token().Encode()) + assert.Equal(t, 100, paginator.Size()) + + opts, err = Parse(url.Values{"page_token": {""}, "page_size": {""}}, NewStringPageToken) + require.NoError(t, err) + paginator = GetPaginator(append(opts, WithDefaultToken(StringPageToken("default2")))...) + assert.Equal(t, "default2", paginator.Token().Encode()) + assert.Equal(t, 100, paginator.Size()) + + opts, err = Parse(url.Values{"page_token": {"", "foo", ""}, "page_size": {"", "123", ""}}, NewStringPageToken) + require.NoError(t, err) + paginator = GetPaginator(append(opts, WithDefaultToken(StringPageToken("default3")))...) + assert.Equal(t, "foo", paginator.Token().Encode()) + assert.Equal(t, 123, paginator.Size()) + }) } func TestPaginateWithAdditionalColumn(t *testing.T) {