failed NO_PATCH NO_PATCH UNSUBMITTED empty_patch(None) · None tool calls · 0 s · fastapi/fastapi
🚸 Improve error message for invalid query parameter type annotations
When I bumped FastAPI from 0.181.1 to 0.181.2, project started crashing on startup with AssertionError. The traceback didn’t provide any helpful context. After debugging, I found that issue was caused by incorrectly typed query parameters after the changes introduced in PR #12942
This PR adds a clearer error message to assertion, indicating that query parameter type is incorrect, and updates tests to ensure that new message is present.
Below is an example of a wrongly typed query parameter that didn’t fail before 0.181.2, but starts failing in 0.181.2 and later:
```python
from typing import Annotated, Any
from fastapi import FastAPI, Query
from pydantic import BeforeValidator, WithJsonSchema
app = FastAPI()
FilterableStr = Annotated[
str | dict,
BeforeValidator(lambda x: x),
WithJsonSchema({"type": "string"}, mode="serialization"),
]
@app.get("/hello")
async def get_hello(data: FilterableStr | None = Query(default=None)) -> Any:
return data
```
Stacktrace before:
```python
File "/Users/tony/Projects/fastapi_exampl/.venv/lib/python3.13/site-packages/fastapi/routing.py", line 630, in __init__
self.dependant = get_dependant(path=self.path_format, call=self.endpoint)
~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/tony/Projects/fastapi_exampl/.venv/lib/python3.13/site-packages/fastapi/dependencies/utils.py", line 293, in get_dependant
param_details = analyze_param(
param_name=param_name,
...<2 lines>...
is_path_param=is_path_param,
)
File "/Users/tony/Projects/fastapi_exampl/.venv/lib/python3.13/site-packages/fastapi/dependencies/utils.py", line 510, in analyze_param
is_scalar_field(field)
...<5 lines>...
)
AssertionError
```
Stacktrace after:
```python
File "/Users/tony/Projects/fastapi_exampl/.venv/lib/python3.13/site-packages/fastapi/dependencies/utils.py", line 293, in get_dependant
param_details = analyze_param(
param_name=param_name,
...<2 lines>...
is_path_param=is_path_param,
)
File "/Users/tony/Projects/fastapi_exampl/.venv/lib/python3.13/site-packages/fastapi/dependencies/utils.py", line 510, in analyze_param
is_scalar_field(field)
...<5 lines>...
)
AssertionError: Query param 'data' must be of one of the supported types
```| # | Tool | Arguments | Result |
|---|---|---|---|
| No trace captured. | |||
F
=================================== FAILURES ===================================
____________________________ test_invalid_sequence _____________________________
def test_invalid_sequence():
> with pytest.raises(
AssertionError,
match="Query parameter 'q' must be one of the supported types",
):
E AssertionError: Regex pattern did not match.
E Expected regex: "Query parameter 'q' must be one of the supported types"
E Actual message: ''
tests/test_invalid_sequence_param.py:9: AssertionError
!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!
1 failed in 0.47s