Skip to content

Commit dfe70bb

Browse files
authored
update OpenAPI document with configured collection limits (#2037)
* update OpenAPI document with configured collection limits * update OpenAPI document with configured collection limits
1 parent 6ee6866 commit dfe70bb

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

pygeoapi/api/itemtypes.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#
3636
# =================================================================
3737

38-
38+
from collections import ChainMap
3939
from copy import deepcopy
4040
from datetime import datetime
4141
from http import HTTPStatus
@@ -1101,6 +1101,21 @@ def get_oas_30(cfg: dict, locale: str) -> tuple[list[dict[str, str]], dict[str,
11011101
}
11021102
}
11031103

1104+
limit = {
1105+
'name': 'limit',
1106+
'in': 'query',
1107+
'description': 'The optional limit parameter limits the number of items that are presented in the response document', # noqa
1108+
'required': False,
1109+
'schema': {
1110+
'type': 'integer',
1111+
'minimum': 1,
1112+
'maximum': 10000,
1113+
'default': 100
1114+
},
1115+
'style': 'form',
1116+
'explode': False
1117+
}
1118+
11041119
profile = {
11051120
'name': 'profile',
11061121
'in': 'query',
@@ -1144,6 +1159,11 @@ def get_oas_30(cfg: dict, locale: str) -> tuple[list[dict[str, str]], dict[str,
11441159

11451160
coll_properties['schema']['items']['enum'] = list(p.fields.keys())
11461161

1162+
coll_limit = _derive_limit(
1163+
deepcopy(limit), cfg['server'].get('limits', {}),
1164+
v.get('limits', {})
1165+
)
1166+
11471167
paths[items_path] = {
11481168
'get': {
11491169
'summary': f'Get {title} items',
@@ -1154,7 +1174,7 @@ def get_oas_30(cfg: dict, locale: str) -> tuple[list[dict[str, str]], dict[str,
11541174
{'$ref': '#/components/parameters/f'},
11551175
{'$ref': '#/components/parameters/lang'},
11561176
{'$ref': '#/components/parameters/bbox'},
1157-
{'$ref': f"{OPENAPI_YAML['oapif-1']}#/components/parameters/limit"}, # noqa
1177+
coll_limit,
11581178
{'$ref': '#/components/parameters/crs'}, # noqa
11591179
{'$ref': '#/components/parameters/bbox-crs'},
11601180
coll_properties,
@@ -1405,3 +1425,28 @@ def get_oas_30(cfg: dict, locale: str) -> tuple[list[dict[str, str]], dict[str,
14051425
LOGGER.debug('collection is not feature/item based')
14061426

14071427
return [{'name': 'records'}, {'name': 'features'}], {'paths': paths}
1428+
1429+
1430+
def _derive_limit(limit_object, server_limits, collection_limits) -> dict:
1431+
"""
1432+
Helper function to derive a limit object for a given collection
1433+
1434+
:param limit_object: OpenAPI limit parameter
1435+
:param server_limits: server level limits configuration
1436+
:param collection_limits: collection level limits configuration
1437+
1438+
:returns: updated limit object
1439+
"""
1440+
1441+
effective_limits = ChainMap(collection_limits, server_limits)
1442+
1443+
default_limit = effective_limits.get('default_items', 10)
1444+
max_limit = effective_limits.get('max_items', 10)
1445+
1446+
limit_object['schema']['default'] = default_limit
1447+
limit_object['schema']['maximum'] = max_limit
1448+
1449+
text = f' (maximum={max_limit}, default={default_limit}).'
1450+
limit_object['description'] += text
1451+
1452+
return limit_object

0 commit comments

Comments
 (0)