diff --git a/netbox/netbox/api/pagination.py b/netbox/netbox/api/pagination.py index 5ecade26402..961d524776d 100644 --- a/netbox/netbox/api/pagination.py +++ b/netbox/netbox/api/pagination.py @@ -38,12 +38,14 @@ def paginate_queryset(self, queryset, request, view=None): def get_limit(self, request): if self.limit_query_param: + MAX_PAGE_SIZE = get_config().MAX_PAGE_SIZE + if MAX_PAGE_SIZE: + MAX_PAGE_SIZE = max(MAX_PAGE_SIZE, self.default_limit) try: limit = int(request.query_params[self.limit_query_param]) if limit < 0: raise ValueError() # Enforce maximum page size, if defined - MAX_PAGE_SIZE = get_config().MAX_PAGE_SIZE if MAX_PAGE_SIZE: return MAX_PAGE_SIZE if limit == 0 else min(limit, MAX_PAGE_SIZE) return limit diff --git a/netbox/utilities/tests/test_api.py b/netbox/utilities/tests/test_api.py index ba0c3c4f82d..2c3ba0566fc 100644 --- a/netbox/utilities/tests/test_api.py +++ b/netbox/utilities/tests/test_api.py @@ -144,6 +144,19 @@ def test_default_page_size(self): self.assertIsNone(response.data['previous']) self.assertEqual(len(response.data['results']), page_size) + @override_settings(MAX_PAGE_SIZE=30) + def test_default_page_size_with_small_max_page_size(self): + response = self.client.get(self.url, format='json', **self.header) + page_size = get_config().MAX_PAGE_SIZE + paginate_count = get_config().PAGINATE_COUNT + self.assertLess(page_size, 100, "Default page size not sufficient for data set") + + self.assertHttpStatus(response, status.HTTP_200_OK) + self.assertEqual(response.data['count'], 100) + self.assertTrue(response.data['next'].endswith(f'?limit={paginate_count}&offset={paginate_count}')) + self.assertIsNone(response.data['previous']) + self.assertEqual(len(response.data['results']), paginate_count) + def test_custom_page_size(self): response = self.client.get(f'{self.url}?limit=10', format='json', **self.header) @@ -153,15 +166,15 @@ def test_custom_page_size(self): self.assertIsNone(response.data['previous']) self.assertEqual(len(response.data['results']), 10) - @override_settings(MAX_PAGE_SIZE=20) + @override_settings(MAX_PAGE_SIZE=80) def test_max_page_size(self): response = self.client.get(f'{self.url}?limit=0', format='json', **self.header) self.assertHttpStatus(response, status.HTTP_200_OK) self.assertEqual(response.data['count'], 100) - self.assertTrue(response.data['next'].endswith('?limit=20&offset=20')) + self.assertTrue(response.data['next'].endswith('?limit=80&offset=80')) self.assertIsNone(response.data['previous']) - self.assertEqual(len(response.data['results']), 20) + self.assertEqual(len(response.data['results']), 80) @override_settings(MAX_PAGE_SIZE=0) def test_max_page_size_disabled(self):