From c770e6b45dba82302ebe9831302bb57082d34509 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 3 Oct 2025 14:22:55 -0700 Subject: [PATCH 1/2] 20496 fix max_page_size for REST API --- contrib/openapi.json | 8 ++++---- netbox/netbox/api/pagination.py | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/contrib/openapi.json b/contrib/openapi.json index 94cf6d6592b..2e0cbaa0791 100644 --- a/contrib/openapi.json +++ b/contrib/openapi.json @@ -214760,7 +214760,7 @@ }, "mark_utilized": { "type": "boolean", - "description": "Report space as 100% utilized" + "description": "Report space as fully utilized" } }, "required": [ @@ -214869,7 +214869,7 @@ }, "mark_utilized": { "type": "boolean", - "description": "Report space as 100% utilized" + "description": "Report space as fully utilized" } }, "required": [ @@ -231880,7 +231880,7 @@ }, "mark_utilized": { "type": "boolean", - "description": "Report space as 100% utilized" + "description": "Report space as fully utilized" } } }, @@ -252203,7 +252203,7 @@ }, "mark_utilized": { "type": "boolean", - "description": "Report space as 100% utilized" + "description": "Report space as fully utilized" } }, "required": [ diff --git a/netbox/netbox/api/pagination.py b/netbox/netbox/api/pagination.py index 698e0a8dda2..1650628a9ce 100644 --- a/netbox/netbox/api/pagination.py +++ b/netbox/netbox/api/pagination.py @@ -44,22 +44,28 @@ def paginate_queryset(self, queryset, request, view=None): return list(queryset[self.offset:]) def get_limit(self, request): + max_limit = self.default_limit + MAX_PAGE_SIZE = get_config().MAX_PAGE_SIZE + if MAX_PAGE_SIZE: + max_limit = min(max_limit, MAX_PAGE_SIZE) + 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 + if MAX_PAGE_SIZE: - return MAX_PAGE_SIZE if limit == 0 else min(limit, MAX_PAGE_SIZE) - return limit + if limit == 0: + max_limit = MAX_PAGE_SIZE + else: + max_limit = min(MAX_PAGE_SIZE, limit) + else: + max_limit = limit except (KeyError, ValueError): pass - return self.default_limit + return max_limit def get_queryset_count(self, queryset): return queryset.count() From 10e8e7b0719d3106a64d26c448cc78b88064598f Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 3 Oct 2025 14:54:08 -0700 Subject: [PATCH 2/2] 20496 fix test --- netbox/utilities/tests/test_api.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/netbox/utilities/tests/test_api.py b/netbox/utilities/tests/test_api.py index b6e5e136046..f36827d8527 100644 --- a/netbox/utilities/tests/test_api.py +++ b/netbox/utilities/tests/test_api.py @@ -149,14 +149,13 @@ def test_default_page_size(self): 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.assertTrue(response.data['next'].endswith(f'?limit={page_size}&offset={page_size}')) self.assertIsNone(response.data['previous']) - self.assertEqual(len(response.data['results']), paginate_count) + self.assertEqual(len(response.data['results']), page_size) def test_custom_page_size(self): response = self.client.get(f'{self.url}?limit=10', format='json', **self.header)