From 09e9cbd927a77b1c528d0ec1a56beadc9198910c Mon Sep 17 00:00:00 2001 From: fullsend-code Date: Fri, 5 Jun 2026 08:27:45 +0000 Subject: [PATCH] fix(#59): clamp meta.last page to minimum of 1 for empty results When a paginated endpoint returns zero items, pagination_query.pages is 0, causing meta.last to point to page=0 which is an invalid URL that returns a 404. Fix by using max(1, pagination_query.pages) so meta.last always points to at least page=1, matching meta.first for empty collections. Add test_get_builds_empty to verify meta.last equals meta.first and contains page=1 when no builds exist. Note: kerberos-dependent test suites could not run in the sandbox (missing libkrb5-dev). The web/API tests covering the changed code passed. Full suite verification is required in CI. Closes #59 --- iib/web/utils.py | 2 +- tests/test_web/test_api_v1.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/iib/web/utils.py b/iib/web/utils.py index 3f3da6f0d..92bdecb92 100644 --- a/iib/web/utils.py +++ b/iib/web/utils.py @@ -26,7 +26,7 @@ def pagination_metadata(pagination_query: Pagination, **kwargs) -> PaginationMet ), 'last': url_for( str(request.endpoint), - page=pagination_query.pages, + page=max(1, pagination_query.pages), per_page=pagination_query.per_page, _external=True, **kwargs, diff --git a/tests/test_web/test_api_v1.py b/tests/test_web/test_api_v1.py index 51b785e44..ef97615ea 100644 --- a/tests/test_web/test_api_v1.py +++ b/tests/test_web/test_api_v1.py @@ -181,6 +181,15 @@ def test_get_builds(app, auth_env, client, db): assert rv_json['items'][0]['user'] == 'tbrady@DOMAIN.LOCAL' +def test_get_builds_empty(client, db): + rv_json = client.get('/api/v1/builds').json + assert rv_json['items'] == [] + assert rv_json['meta']['total'] == 0 + assert rv_json['meta']['pages'] == 0 + assert rv_json['meta']['last'] == rv_json['meta']['first'] + assert 'page=1' in rv_json['meta']['last'] + + def test_index_image_filter( app, client, db, minimal_request_add, minimal_request_rm, minimal_request_fbc_operations ):