Skip to content
Open
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
6 changes: 5 additions & 1 deletion do/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,9 @@ func lastPage(resp *godo.Response) (int, error) {
return 0, fmt.Errorf("could not find page param: %v", err)
}

return page, err
if page < 1 {
return 1, nil
}

return page, nil
}
16 changes: 16 additions & 0 deletions do/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,19 @@ func Test_Pagination_lastPage(t *testing.T) {
}
}
}

func Test_PaginateResp_emptyListWithZeroLastPage(t *testing.T) {
// Regression test for https://github.com/digitalocean/doctl/issues/1888:
// a Last page link whose page param parses to 0 (as with an empty
// collection) made lastPage return 0, so set(1, ...) panicked indexing
// a zero-length page list. lastPage clamps such values to 1.
resp := &godo.Response{Links: &godo.Links{Pages: &godo.Pages{Last: "http://example.com/?page=0"}}}

gen := func(*godo.ListOptions) ([]any, *godo.Response, error) {
return []any{}, resp, nil
}

list, err := PaginateResp(gen)
assert.NoError(t, err)
assert.Empty(t, list)
}