From d9a3c4cf4bf4937c51604e939ccc17de9e361048 Mon Sep 17 00:00:00 2001 From: Dave Date: Tue, 30 Apr 2024 19:56:38 +0200 Subject: [PATCH] fix offset paginator --- openapi_python_client/parser/pagination.py | 4 +++- openapi_python_client/templates/source.py.jinja | 7 ++++++- tests/renderer/test_pokemon_cases_parser.py | 16 ++++++++++++++-- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/openapi_python_client/parser/pagination.py b/openapi_python_client/parser/pagination.py index dd0c5b8..6ae6c17 100644 --- a/openapi_python_client/parser/pagination.py +++ b/openapi_python_client/parser/pagination.py @@ -84,13 +84,15 @@ def from_endpoint(cls, endpoint: "Endpoint") -> Optional["Pagination"]: # When spec doesn't provide default/max limit, fallback to a conservative default # 20 should be safe for most APIs limit_initial = int(limit_param.maximum) if limit_param.maximum else (limit_param.default or 20) + total_prop = resp.content_schema.crawled_properties.find_property(RE_TOTAL_PROPERTY, require_type="integer") - if offset_param and limit_param and limit_initial: + if offset_param and limit_param and limit_initial and total_prop: pagination_config = { "type": "offset", "initial_limit": limit_initial, "offset_param": offset_param.name, "limit_param": limit_param.name, + "total_path": total_prop.json_path, } return cls( paginator_config=pagination_config, diff --git a/openapi_python_client/templates/source.py.jinja b/openapi_python_client/templates/source.py.jinja index 32b2d53..4b67535 100644 --- a/openapi_python_client/templates/source.py.jinja +++ b/openapi_python_client/templates/source.py.jinja @@ -42,7 +42,12 @@ def {{ source_name }}( {% if endpoint.pagination_args %} "paginator": { {% for key, value in endpoint.pagination_args.items() %} - "{{key}}": "{{value}}", + "{{key}}": + {% if value is integer %} + {{value}}, + {% else %} + "{{value}}", + {% endif %} {% endfor %} }, {% endif %} diff --git a/tests/renderer/test_pokemon_cases_parser.py b/tests/renderer/test_pokemon_cases_parser.py index 8d6b567..dd91fcb 100644 --- a/tests/renderer/test_pokemon_cases_parser.py +++ b/tests/renderer/test_pokemon_cases_parser.py @@ -34,7 +34,13 @@ def test_paged_poke_load() -> None: "endpoint": { "path": "/api/v2/pokemon/", "data_selector": "results", - "paginator": {"initial_limit": "20", "limit_param": "limit", "offset_param": "offset", "type": "offset"}, + "paginator": { + "initial_limit": 20, + "limit_param": "limit", + "offset_param": "offset", + "type": "offset", + "total_path": "count", + }, "path": "/api/v2/pokemon/", }, } @@ -54,7 +60,13 @@ def test_simple_child_table_poke_load() -> None: "endpoint": { "path": "/api/v2/pokemon/", "data_selector": "results", - "paginator": {"initial_limit": "20", "limit_param": "limit", "offset_param": "offset", "type": "offset"}, + "paginator": { + "initial_limit": 20, + "limit_param": "limit", + "offset_param": "offset", + "type": "offset", + "total_path": "count", + }, }, }