Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consider only non-empty values when parsing pagination parameters from URLs #752

Merged
merged 2 commits into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions pagination/keysetpagination/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strings"

"github.com/pkg/errors"

"github.com/ory/x/stringsx"
)

// Pagination Request Parameters
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
20 changes: 20 additions & 0 deletions pagination/keysetpagination/paginator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Loading