2
from fastapi import Depends, FastAPI, Path
3
from fastapi.param_functions import Query
4
from fastapi.testclient import TestClient
5
from fastapi.utils import PYDANTIC_V2
6
from typing_extensions import Annotated
11
def test_no_annotated_defaults():
13
AssertionError, match="Path parameters cannot have a default value"
16
@app.get("/items/{item_id}/")
17
async def get_item(item_id: Annotated[int, Path(default=1)]):
18
pass # pragma: nocover
23
"`Query` default value cannot be set in `Annotated` for 'item_id'. Set the"
24
" default value with `=` instead."
29
async def get(item_id: Annotated[int, Query(default=1)]):
30
pass # pragma: nocover
33
def test_multiple_annotations():
35
pass # pragma: nocover
37
@app.get("/multi-query")
38
async def get(foo: Annotated[int, Query(gt=2), Query(lt=10)]):
44
"Cannot specify `Depends` in `Annotated` and default value"
50
async def get2(foo: Annotated[int, Depends(dep)] = Depends(dep)):
51
pass # pragma: nocover
56
"Cannot specify a FastAPI annotation in `Annotated` and `Depends` as a"
57
" default value together for 'foo'"
62
async def get3(foo: Annotated[int, Query(min_length=1)] = Depends(dep)):
63
pass # pragma: nocover
65
client = TestClient(app)
66
response = client.get("/multi-query", params={"foo": "5"})
67
assert response.status_code == 200
68
assert response.json() == 5
70
response = client.get("/multi-query", params={"foo": "123"})
71
assert response.status_code == 422
74
response = client.get("/multi-query", params={"foo": "1"})
75
assert response.status_code == 422