Skip to content

Commit b87dea9

Browse files
authored
chore: added API validation for numeric values (#826)
* chore: added numeric values validation in API * chore: updated tests * chore: regenerate client * fix: fixed exception handling * fix: client regeneration * fix: regenerate client * fix: reverted HTTPException handling * fix: added status_code checks in tests * fix: gubbins client regen * fix: diracx client regen * fix: gubbins client regen
1 parent a59841d commit b87dea9

4 files changed

Lines changed: 18 additions & 15 deletions

File tree

diracx-client/src/diracx/client/_generated/operations/_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ def build_jobs_search_request(*, page: int = 1, per_page: int = 100, **kwargs: A
519519

520520
# Construct parameters
521521
if page is not None:
522-
_params["page"] = _SERIALIZER.query("page", page, "int")
522+
_params["page"] = _SERIALIZER.query("page", page, "int", minimum=1)
523523
if per_page is not None:
524-
_params["per_page"] = _SERIALIZER.query("per_page", per_page, "int")
524+
_params["per_page"] = _SERIALIZER.query("per_page", per_page, "int", maximum=10000, minimum=1)
525525

526526
# Construct headers
527527
if content_type is not None:

diracx-routers/src/diracx/routers/jobs/query.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@
33
from http import HTTPStatus
44
from typing import Annotated, Any
55

6-
from fastapi import Body, Depends, Response
6+
from fastapi import Body, Depends, Query, Response
77

88
from diracx.core.models.search import (
99
SearchParams,
1010
SummaryParams,
1111
)
1212
from diracx.core.properties import JOB_ADMINISTRATOR
13+
from diracx.logic.jobs.query import MAX_PER_PAGE
1314
from diracx.logic.jobs.query import search as search_bl
1415
from diracx.logic.jobs.query import summary as summary_bl
1516

@@ -25,10 +26,6 @@
2526

2627
router = DiracxRouter()
2728

28-
29-
MAX_PER_PAGE = 10000
30-
31-
3229
EXAMPLE_SEARCHES = {
3330
"Show all": {
3431
"summary": "Show all",
@@ -132,8 +129,8 @@ async def search(
132129
user_info: Annotated[AuthorizedUserInfo, Depends(verify_dirac_access_token)],
133130
check_permissions: CheckWMSPolicyCallable,
134131
response: Response,
135-
page: int = 1,
136-
per_page: int = 100,
132+
page: Annotated[int, Query(ge=1)] = 1,
133+
per_page: Annotated[int, Query(ge=1, le=MAX_PER_PAGE)] = 100,
137134
body: Annotated[
138135
SearchParams | None, Body(openapi_examples=EXAMPLE_SEARCHES)
139136
] = None,

diracx-routers/tests/jobs/test_query.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,19 +525,25 @@ def test_search_pagination(normal_user_client):
525525

526526
# Set the per_page parameter to 0
527527
r = normal_user_client.post("/api/jobs/search", params={"page": 1, "per_page": 0})
528-
assert r.status_code == 400, r.json()
528+
assert r.status_code == 422, r.json()
529529

530530
# Set the per_page parameter to a negative number
531531
r = normal_user_client.post("/api/jobs/search", params={"page": 1, "per_page": -1})
532-
assert r.status_code == 400, r.json()
532+
assert r.status_code == 422, r.json()
533533

534534
# Set the page parameter to 0
535535
r = normal_user_client.post("/api/jobs/search", params={"page": 0, "per_page": 10})
536-
assert r.status_code == 400, r.json()
536+
assert r.status_code == 422, r.json()
537537

538538
# Set the page parameter to a negative number
539539
r = normal_user_client.post("/api/jobs/search", params={"page": -1, "per_page": 10})
540-
assert r.status_code == 400, r.json()
540+
assert r.status_code == 422, r.json()
541+
542+
# Too many jobs per page (max = 10000)
543+
r = normal_user_client.post(
544+
"/api/jobs/search", params={"page": 1, "per_page": 20000}
545+
)
546+
assert r.status_code == 422, r.json()
541547

542548

543549
def test_user_cannot_submit_parametric_jdl_greater_than_max_parametric_jobs(

extensions/gubbins/gubbins-client/src/gubbins/client/_generated/operations/_operations.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,9 @@ def build_jobs_search_request(*, page: int = 1, per_page: int = 100, **kwargs: A
519519

520520
# Construct parameters
521521
if page is not None:
522-
_params["page"] = _SERIALIZER.query("page", page, "int")
522+
_params["page"] = _SERIALIZER.query("page", page, "int", minimum=1)
523523
if per_page is not None:
524-
_params["per_page"] = _SERIALIZER.query("per_page", per_page, "int")
524+
_params["per_page"] = _SERIALIZER.query("per_page", per_page, "int", maximum=10000, minimum=1)
525525

526526
# Construct headers
527527
if content_type is not None:

0 commit comments

Comments
 (0)