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(pagination): always enforce max size is set #820

Merged
merged 1 commit into from
Nov 6, 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
11 changes: 9 additions & 2 deletions pagination/keysetpagination/paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ var ErrUnknownOrder = errors.New("unknown order")
const (
OrderDescending Order = "DESC"
OrderAscending Order = "ASC"

DefaultSize = 100
DefaultMaxSize = 500
)

func (o Order) extract() (string, string, error) {
Expand Down Expand Up @@ -62,7 +65,7 @@ func (p *Paginator) Size() int {
size = 100
}
}
if p.maxSize > 0 && size > p.maxSize {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If for whatever reason p.maxSize is 0, we would set the size to 0 which is fine in this case.

if size > p.maxSize {
size = p.maxSize
}
return size
Expand Down Expand Up @@ -229,7 +232,11 @@ func withIsLast(isLast bool) Option {
}

func GetPaginator(modifiers ...Option) *Paginator {
opts := &Paginator{}
opts := &Paginator{
// these can still be overridden by the modifiers, but they should never be unset
maxSize: DefaultMaxSize,
defaultSize: DefaultSize,
}
for _, f := range modifiers {
opts = f(opts)
}
Expand Down
5 changes: 5 additions & 0 deletions pagination/keysetpagination/paginator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func TestPaginator(t *testing.T) {
opts: nil,
expectedSize: 100,
},
{
name: "default max size",
opts: []Option{WithSize(1000)},
expectedSize: DefaultMaxSize,
},
{
name: "with size and token",
opts: []Option{WithSize(10), WithToken(StringPageToken("token"))},
Expand Down
Loading