Skip to content

Commit edc97e9

Browse files
authored
Replace values above limit with limit value. (#526)
* Set limit to max value rather than error. Fixes #496. * Update limit tests. * Update changelog.
1 parent 955c34e commit edc97e9

File tree

5 files changed

+52
-5
lines changed

5 files changed

+52
-5
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
* Updated CI to test against [pgstac v0.6.12](https://github.com/stac-utils/pgstac/releases/tag/v0.6.12) ([#511](https://github.com/stac-utils/stac-fastapi/pull/511))
1313
* Reworked `update_openapi` and added a test for it ([#523](https://github.com/stac-utils/stac-fastapi/pull/523))
14+
* Limit values above 10,000 are now replaced with 10,000 instead of returning a 400 error ([#526](https://github.com/stac-utils/stac-fastapi/pull/526))
1415

1516
### Removed
1617

stac_fastapi/pgstac/tests/api/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ async def test_app_query_extension_limit_gt10000(
202202

203203
params = {"limit": 10001}
204204
resp = await app_client.post("/search", json=params)
205-
assert resp.status_code == 400
205+
assert resp.status_code == 200
206206

207207

208208
async def test_app_query_extension_gt(load_test_data, app_client, load_test_collection):

stac_fastapi/sqlalchemy/tests/api/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ def test_app_query_extension_limit_gt10000(
209209

210210
params = {"limit": 10001}
211211
resp = app_client.post("/search", json=params)
212-
assert resp.status_code == 400
212+
assert resp.status_code == 200
213213

214214

215215
def test_app_query_extension_limit_10000(

stac_fastapi/types/stac_fastapi/types/search.py

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from datetime import datetime
99
from enum import auto
1010
from types import DynamicClassAttribute
11-
from typing import Any, Callable, Dict, List, Optional, Union
11+
from typing import Any, Callable, Dict, Generator, List, Optional, Union
1212

1313
import attr
1414
from geojson_pydantic.geometries import (
@@ -20,7 +20,9 @@
2020
Polygon,
2121
_GeometryBase,
2222
)
23-
from pydantic import BaseModel, conint, validator
23+
from pydantic import BaseModel, ConstrainedInt, validator
24+
from pydantic.errors import NumberNotGtError
25+
from pydantic.validators import int_validator
2426
from stac_pydantic.shared import BBox
2527
from stac_pydantic.utils import AutoValueEnum
2628

@@ -30,6 +32,28 @@
3032
NumType = Union[float, int]
3133

3234

35+
class Limit(ConstrainedInt):
36+
"""An positive integer that maxes out at 10,000."""
37+
38+
ge: int = 1
39+
le: int = 10_000
40+
41+
@classmethod
42+
def __get_validators__(cls) -> Generator[Callable[..., Any], None, None]:
43+
"""Yield the relevant validators."""
44+
yield int_validator
45+
yield cls.validate
46+
47+
@classmethod
48+
def validate(cls, value: int) -> int:
49+
"""Validate the integer value."""
50+
if value < cls.ge:
51+
raise NumberNotGtError(limit_value=cls.ge)
52+
if value > cls.le:
53+
return cls.le
54+
return value
55+
56+
3357
class Operator(str, AutoValueEnum):
3458
"""Defines the set of operators supported by the API."""
3559

@@ -97,7 +121,7 @@ class BaseSearchPostRequest(BaseModel):
97121
Union[Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon]
98122
]
99123
datetime: Optional[str]
100-
limit: Optional[conint(gt=0, le=10000)] = 10
124+
limit: Optional[Limit] = 10
101125

102126
@property
103127
def start_date(self) -> Optional[datetime]:
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
from pydantic import ValidationError
3+
4+
from stac_fastapi.types.search import BaseSearchPostRequest
5+
6+
7+
@pytest.mark.parametrize("value", [0, -1])
8+
def test_limit_ge(value):
9+
with pytest.raises(ValidationError):
10+
BaseSearchPostRequest(limit=value)
11+
12+
13+
@pytest.mark.parametrize("value", [1, 10_000])
14+
def test_limit(value):
15+
search = BaseSearchPostRequest(limit=value)
16+
assert search.limit == value
17+
18+
19+
@pytest.mark.parametrize("value", [10_001, 100_000, 1_000_000])
20+
def test_limit_le(value):
21+
search = BaseSearchPostRequest(limit=value)
22+
assert search.limit == 10_000

0 commit comments

Comments
 (0)