diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 0255cfc7f..f45db31d6 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -572,7 +572,7 @@ class CursorPagination(BasePagination): if not self.has_next: return None - if self.cursor and self.cursor.reverse and self.cursor.offset != 0: + if self.cursor and self.cursor.reverse and self.cursor.offset != 0 and self.page: # If we're reversing direction and we have an offset cursor # then we cannot use the first position we find as a marker. compare = self._get_position_from_instance(self.page[-1], self.ordering) @@ -620,7 +620,7 @@ class CursorPagination(BasePagination): if not self.has_previous: return None - if self.cursor and not self.cursor.reverse and self.cursor.offset != 0: + if self.cursor and not self.cursor.reverse and self.cursor.offset != 0 and self.page: # If we're reversing direction and we have an offset cursor # then we cannot use the first position we find as a marker. compare = self._get_position_from_instance(self.page[0], self.ordering) diff --git a/tests/test_pagination.py b/tests/test_pagination.py index dd7f70330..3e8f4b9fa 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -9,7 +9,7 @@ from django.test import TestCase from rest_framework import ( exceptions, filters, generics, pagination, serializers, status ) -from rest_framework.pagination import PAGE_BREAK, PageLink +from rest_framework.pagination import PAGE_BREAK, PageLink, Cursor from rest_framework.request import Request from rest_framework.test import APIRequestFactory @@ -633,6 +633,16 @@ class CursorPaginationTestsMixin: assert isinstance(self.pagination.to_html(), type('')) + def test_offset_cutoff(self): + (previous, current, next, previous_url, next_url) = self.get_pages('/') + + next_url = self.pagination.encode_cursor(Cursor(1050, False, None)) + (previous, current, next, previous_url, next_url) = self.get_pages(next_url) + + assert previous == [7, 7, 7, 8, 9] + assert current == [] + assert next is None + class TestCursorPagination(CursorPaginationTestsMixin): """