From 7d2335cf3f565733ac2c78bfb69b27d6d7d2e38a Mon Sep 17 00:00:00 2001 From: Huba Tuba <57007485+floxay@users.noreply.github.com> Date: Wed, 14 Feb 2024 15:14:56 +0100 Subject: [PATCH] feat: only display path in ValidationExceptions (#3064) --- litestar/_kwargs/extractors.py | 7 ++++++- litestar/_signature/model.py | 7 ++++++- tests/unit/test_kwargs/test_layered_params.py | 2 +- tests/unit/test_signature/test_validation.py | 4 ++-- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/litestar/_kwargs/extractors.py b/litestar/_kwargs/extractors.py index bf6f34a4ec..89f90cedfd 100644 --- a/litestar/_kwargs/extractors.py +++ b/litestar/_kwargs/extractors.py @@ -11,6 +11,7 @@ ) from litestar.datastructures import Headers from litestar.datastructures.upload_file import UploadFile +from litestar.datastructures.url import URL from litestar.enums import ParamType, RequestEncodingType from litestar.exceptions import ValidationException from litestar.params import BodyKwarg @@ -106,8 +107,12 @@ def extractor(values: dict[str, Any], connection: ASGIConnection) -> None: values.update(connection_mapping) except KeyError as e: param = alias_to_params[e.args[0]] + path = URL.from_components( + path=connection.url.path, + query=connection.url.query, + ) raise ValidationException( - f"Missing required {param.param_type.value} parameter {param.field_alias!r} for url {connection.url}" + f"Missing required {param.param_type.value} parameter {param.field_alias!r} for path {path}" ) from e return extractor diff --git a/litestar/_signature/model.py b/litestar/_signature/model.py index f3b37ea701..42c79947f5 100644 --- a/litestar/_signature/model.py +++ b/litestar/_signature/model.py @@ -32,6 +32,7 @@ _validate_signature_dependencies, ) from litestar.datastructures.state import ImmutableState +from litestar.datastructures.url import URL from litestar.dto import AbstractDTO, DTOData from litestar.enums import ParamType, ScopeType from litestar.exceptions import InternalServerException, ValidationException @@ -119,7 +120,11 @@ def _create_exception(cls, connection: ASGIConnection, messages: list[ErrorMessa for err_message in messages if ("key" in err_message and err_message["key"] not in cls._dependency_name_set) or "key" not in err_message ]: - return ValidationException(detail=f"Validation failed for {method} {connection.url}", extra=client_errors) + path = URL.from_components( + path=connection.url.path, + query=connection.url.query, + ) + return ValidationException(detail=f"Validation failed for {method} {path}", extra=client_errors) return InternalServerException() @classmethod diff --git a/tests/unit/test_kwargs/test_layered_params.py b/tests/unit/test_kwargs/test_layered_params.py index 201ad78277..9b20222063 100644 --- a/tests/unit/test_kwargs/test_layered_params.py +++ b/tests/unit/test_kwargs/test_layered_params.py @@ -115,7 +115,7 @@ def my_handler(self) -> dict: response = client.get("/router/controller/1", params=query, headers=headers) assert response.status_code == HTTP_400_BAD_REQUEST - assert response.json()["detail"].startswith(f"Missing required {param_type} parameter '{parameter}' for url") + assert response.json()["detail"].startswith(f"Missing required {param_type} parameter '{parameter}' for path") def test_layered_parameters_defaults_and_overrides() -> None: diff --git a/tests/unit/test_signature/test_validation.py b/tests/unit/test_signature/test_validation.py index 70c28203fe..0b1930497d 100644 --- a/tests/unit/test_signature/test_validation.py +++ b/tests/unit/test_signature/test_validation.py @@ -74,7 +74,7 @@ def test(dep: int, param: int, optional_dep: Optional[int] = Dependency()) -> No response = client.get("/?param=thirteen") assert response.json() == { - "detail": "Validation failed for GET http://testserver.local/?param=thirteen", + "detail": "Validation failed for GET /?param=thirteen", "extra": [{"key": "param", "message": "Expected `int`, got `str`", "source": "query"}], "status_code": 400, } @@ -94,7 +94,7 @@ def test(dep: int, param: int, optional_dep: Optional[int] = Dependency()) -> No response = client.get("/?param=thirteen") assert response.json() == { - "detail": "Validation failed for GET http://testserver.local/?param=thirteen", + "detail": "Validation failed for GET /?param=thirteen", "extra": [{"key": "param", "message": "Expected `int`, got `str`", "source": "query"}], "status_code": 400, }