Skip to content

Commit

Permalink
feat: use "path" as an error message source
Browse files Browse the repository at this point in the history
  • Loading branch information
250MHz committed Jan 3, 2025
1 parent ebdb6e5 commit 5cafaf9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions litestar/_signature/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def _build_error_message(cls, keys: Sequence[str], exc_msg: str, connection: ASG
message["source"] = "body"
elif key in connection.query_params:
message["source"] = ParamType.QUERY
elif key in connection.path_params:
message["source"] = ParamType.PATH

elif key in cls._fields and isinstance(cls._fields[key].kwarg_definition, ParameterKwarg):
if cast(ParameterKwarg, cls._fields[key].kwarg_definition).cookie:
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_signature/test_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,20 @@ def test(dep: int, param: int, optional_dep: Optional[int] = Dependency()) -> No
}


def test_invalid_path_parameter() -> None:
@get("/{param:int}")
def test(param: Annotated[int, Parameter(le=10)]) -> None: ...

with create_test_client(route_handlers=[test]) as client:
response = client.get("/11")

assert response.json() == {
"detail": "Validation failed for GET /11",
"extra": [{"key": "param", "message": "Expected `int` <= 10", "source": "path"}],
"status_code": 400,
}


def test_client_backend_error_precedence_over_server_error() -> None:
dependencies = {
"dep": Provide(lambda: "thirteen", sync_to_thread=False),
Expand Down

0 comments on commit 5cafaf9

Please sign in to comment.