API pagination include `rel="next"` when set is empty
Discovered in gitlab-com/infrastructure#2532.
It appears that there is a problem with the GitLab API v3 pagination (maybe v4 as well). If a user has no groups, an empty set is correctly returned, but a “next” link is still provided in the “Link” header:
GET /api/v3/groups?page=1&per_page=10 HTTP/1.1 200 OK Server: nginx Date: Thu, 17 Aug 2017 08:26:55 GMT Content-Type: application/json Content-Length: 2 Cache-Control: max-age=0, private, must-revalidate Etag: W/"d751713988987e9331980363e24189ce" Link: <https://gitlab.com/api/v3/groups?order_by=name&page=2&per_page=10&sort=asc&statistics=false>; rel="next", <https://gitlab.com/api/v3/groups?order_by=name&page=1&per_page=10&sort=asc&statistics=false>; rel="first", <https://gitlab.com/api/v3/groups?order_by=name&page=0&per_page=10&sort=asc&statistics=false>; rel="last" Vary: Origin X-Frame-Options: SAMEORIGIN X-Next-Page: X-Page: 1 X-Per-Page: 10 X-Prev-Page: X-Request-Id: 39b15211-7f6a-40e8-8199-ef0e3f18f8b1 X-Runtime: 0.099849 X-Total: 0 X-Total-Pages: 0 Strict-Transport-Security: max-age=31536000 
And of course, we happily follow the next link, which will return an empty set with another next link and so on, producing an infinite loop.
5 Minutes is the threshold we use to prevent background updates.
Steps to reproduce
- Create a user that does not belong to any group
- Fetch the groups from the API for that user
What is the current bug behavior?
rel="next" is included in the
What is the expected correct behavior?
rel="next" should not be included.
Output of checks
This bug happens on GitLab.com