Skip to content

Commit

Permalink
fix offset paginator
Browse files Browse the repository at this point in the history
  • Loading branch information
sh-rp committed Apr 30, 2024
1 parent 9636190 commit d9a3c4c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 3 additions & 1 deletion openapi_python_client/parser/pagination.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
7 changes: 6 additions & 1 deletion openapi_python_client/templates/source.py.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
Expand Down
16 changes: 14 additions & 2 deletions tests/renderer/test_pokemon_cases_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/",
},
}
Expand All @@ -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",
},
},
}

Expand Down

0 comments on commit d9a3c4c

Please sign in to comment.