mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-18 13:27:45 -04:00
✨ Add support for not needing ... as default value in required Query(), Path(), Header(), etc. (#4906)
* ✨ Do not require default value in Query(), Path(), Header(), etc * 📝 Update source examples for docs with default and required values * ✅ Update tests with new default values and not required Ellipsis * 📝 Update docs for Query params and update info about default value, required, Ellipsis
This commit is contained in:
committed by
GitHub
parent
31690dda2c
commit
9262fa8362
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI, status
|
||||
from fastapi.responses import JSONResponse
|
||||
@@ -10,7 +10,9 @@ items = {"foo": {"name": "Fighters", "size": 6}, "bar": {"name": "Tenders", "siz
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def upsert_item(
|
||||
item_id: str, name: Optional[str] = Body(None), size: Optional[int] = Body(None)
|
||||
item_id: str,
|
||||
name: Union[str, None] = Body(default=None),
|
||||
size: Union[int, None] = Body(default=None),
|
||||
):
|
||||
if item_id in items:
|
||||
item = items[item_id]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Header, HTTPException
|
||||
from pydantic import BaseModel
|
||||
@@ -16,11 +16,11 @@ app = FastAPI()
|
||||
class Item(BaseModel):
|
||||
id: str
|
||||
title: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=Item)
|
||||
async def read_main(item_id: str, x_token: str = Header(...)):
|
||||
async def read_main(item_id: str, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item_id not in fake_db:
|
||||
@@ -29,7 +29,7 @@ async def read_main(item_id: str, x_token: str = Header(...)):
|
||||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item, x_token: str = Header(...)):
|
||||
async def create_item(item: Item, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item.id in fake_db:
|
||||
|
||||
@@ -18,7 +18,7 @@ class Item(BaseModel):
|
||||
|
||||
|
||||
@app.get("/items/{item_id}", response_model=Item)
|
||||
async def read_main(item_id: str, x_token: str = Header(...)):
|
||||
async def read_main(item_id: str, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item_id not in fake_db:
|
||||
@@ -27,7 +27,7 @@ async def read_main(item_id: str, x_token: str = Header(...)):
|
||||
|
||||
|
||||
@app.post("/items/", response_model=Item)
|
||||
async def create_item(item: Item, x_token: str = Header(...)):
|
||||
async def create_item(item: Item, x_token: str = Header()):
|
||||
if x_token != fake_secret_token:
|
||||
raise HTTPException(status_code=400, detail="Invalid X-Token header")
|
||||
if item.id in fake_db:
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from fastapi import Header, HTTPException
|
||||
|
||||
|
||||
async def get_token_header(x_token: str = Header(...)):
|
||||
async def get_token_header(x_token: str = Header()):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel, Field
|
||||
@@ -8,14 +8,14 @@ app = FastAPI()
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = Field(
|
||||
None, title="The description of the item", max_length=300
|
||||
description: Union[str, None] = Field(
|
||||
default=None, title="The description of the item", max_length=300
|
||||
)
|
||||
price: float = Field(..., gt=0, description="The price must be greater than zero")
|
||||
tax: Optional[float] = None
|
||||
price: float = Field(gt=0, description="The price must be greater than zero")
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
||||
@@ -7,13 +7,13 @@ app = FastAPI()
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: str | None = Field(
|
||||
None, title="The description of the item", max_length=300
|
||||
default=None, title="The description of the item", max_length=300
|
||||
)
|
||||
price: float = Field(..., gt=0, description="The price must be greater than zero")
|
||||
price: float = Field(gt=0, description="The price must be greater than zero")
|
||||
tax: float | None = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Path
|
||||
from pydantic import BaseModel
|
||||
@@ -8,17 +8,17 @@ app = FastAPI()
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
|
||||
q: Optional[str] = None,
|
||||
item: Optional[Item] = None,
|
||||
item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
|
||||
q: Union[str, None] = None,
|
||||
item: Union[Item, None] = None,
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
||||
@@ -14,7 +14,7 @@ class Item(BaseModel):
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
|
||||
item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
|
||||
q: str | None = None,
|
||||
item: Item | None = None,
|
||||
):
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel
|
||||
@@ -8,19 +8,17 @@ app = FastAPI()
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
username: str
|
||||
full_name: Optional[str] = None
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
item_id: int, item: Item, user: User, importance: int = Body(...)
|
||||
):
|
||||
async def update_item(item_id: int, item: Item, user: User, importance: int = Body()):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
return results
|
||||
|
||||
@@ -17,8 +17,6 @@ class User(BaseModel):
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(
|
||||
item_id: int, item: Item, user: User, importance: int = Body(...)
|
||||
):
|
||||
async def update_item(item_id: int, item: Item, user: User, importance: int = Body()):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
return results
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel
|
||||
@@ -8,14 +8,14 @@ app = FastAPI()
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
username: str
|
||||
full_name: Optional[str] = None
|
||||
full_name: Union[str, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
@@ -24,8 +24,8 @@ async def update_item(
|
||||
item_id: int,
|
||||
item: Item,
|
||||
user: User,
|
||||
importance: int = Body(..., gt=0),
|
||||
q: Optional[str] = None
|
||||
importance: int = Body(gt=0),
|
||||
q: Union[str, None] = None
|
||||
):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
if q:
|
||||
|
||||
@@ -22,7 +22,7 @@ async def update_item(
|
||||
item_id: int,
|
||||
item: Item,
|
||||
user: User,
|
||||
importance: int = Body(..., gt=0),
|
||||
importance: int = Body(gt=0),
|
||||
q: str | None = None
|
||||
):
|
||||
results = {"item_id": item_id, "item": item, "user": user, "importance": importance}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Body, FastAPI
|
||||
from pydantic import BaseModel
|
||||
@@ -8,12 +8,12 @@ app = FastAPI()
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Optional[float] = None
|
||||
tax: Union[float, None] = None
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
||||
@@ -12,6 +12,6 @@ class Item(BaseModel):
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
async def update_item(item_id: int, item: Item = Body(..., embed=True)):
|
||||
async def update_item(item_id: int, item: Item = Body(embed=True)):
|
||||
results = {"item_id": item_id, "item": item}
|
||||
return results
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import Cookie, FastAPI
|
||||
|
||||
@@ -6,5 +6,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(ads_id: Optional[str] = Cookie(None)):
|
||||
async def read_items(ads_id: Union[str, None] = Cookie(default=None)):
|
||||
return {"ads_id": ads_id}
|
||||
|
||||
@@ -4,5 +4,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(ads_id: str | None = Cookie(None)):
|
||||
async def read_items(ads_id: str | None = Cookie(default=None)):
|
||||
return {"ads_id": ads_id}
|
||||
|
||||
@@ -31,5 +31,5 @@ app.router.route_class = GzipRoute
|
||||
|
||||
|
||||
@app.post("/sum")
|
||||
async def sum_numbers(numbers: List[int] = Body(...)):
|
||||
async def sum_numbers(numbers: List[int] = Body()):
|
||||
return {"sum": sum(numbers)}
|
||||
|
||||
@@ -25,5 +25,5 @@ app.router.route_class = ValidationErrorLoggingRoute
|
||||
|
||||
|
||||
@app.post("/")
|
||||
async def sum_numbers(numbers: List[int] = Body(...)):
|
||||
async def sum_numbers(numbers: List[int] = Body()):
|
||||
return sum(numbers)
|
||||
|
||||
@@ -10,7 +10,7 @@ def query_extractor(q: Optional[str] = None):
|
||||
|
||||
|
||||
def query_or_cookie_extractor(
|
||||
q: str = Depends(query_extractor), last_query: Optional[str] = Cookie(None)
|
||||
q: str = Depends(query_extractor), last_query: Optional[str] = Cookie(default=None)
|
||||
):
|
||||
if not q:
|
||||
return last_query
|
||||
|
||||
@@ -8,7 +8,7 @@ def query_extractor(q: str | None = None):
|
||||
|
||||
|
||||
def query_or_cookie_extractor(
|
||||
q: str = Depends(query_extractor), last_query: str | None = Cookie(None)
|
||||
q: str = Depends(query_extractor), last_query: str | None = Cookie(default=None)
|
||||
):
|
||||
if not q:
|
||||
return last_query
|
||||
|
||||
@@ -3,12 +3,12 @@ from fastapi import Depends, FastAPI, Header, HTTPException
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
async def verify_token(x_token: str = Header(...)):
|
||||
async def verify_token(x_token: str = Header()):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
||||
async def verify_key(x_key: str = Header(...)):
|
||||
async def verify_key(x_key: str = Header()):
|
||||
if x_key != "fake-super-secret-key":
|
||||
raise HTTPException(status_code=400, detail="X-Key header invalid")
|
||||
return x_key
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from fastapi import Depends, FastAPI, Header, HTTPException
|
||||
|
||||
|
||||
async def verify_token(x_token: str = Header(...)):
|
||||
async def verify_token(x_token: str = Header()):
|
||||
if x_token != "fake-super-secret-token":
|
||||
raise HTTPException(status_code=400, detail="X-Token header invalid")
|
||||
|
||||
|
||||
async def verify_key(x_key: str = Header(...)):
|
||||
async def verify_key(x_key: str = Header()):
|
||||
if x_key != "fake-super-secret-key":
|
||||
raise HTTPException(status_code=400, detail="X-Key header invalid")
|
||||
return x_key
|
||||
|
||||
@@ -10,10 +10,10 @@ app = FastAPI()
|
||||
@app.put("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: UUID,
|
||||
start_datetime: Optional[datetime] = Body(None),
|
||||
end_datetime: Optional[datetime] = Body(None),
|
||||
repeat_at: Optional[time] = Body(None),
|
||||
process_after: Optional[timedelta] = Body(None),
|
||||
start_datetime: Optional[datetime] = Body(default=None),
|
||||
end_datetime: Optional[datetime] = Body(default=None),
|
||||
repeat_at: Optional[time] = Body(default=None),
|
||||
process_after: Optional[timedelta] = Body(default=None),
|
||||
):
|
||||
start_process = start_datetime + process_after
|
||||
duration = end_datetime - start_process
|
||||
|
||||
@@ -9,10 +9,10 @@ app = FastAPI()
|
||||
@app.put("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: UUID,
|
||||
start_datetime: datetime | None = Body(None),
|
||||
end_datetime: datetime | None = Body(None),
|
||||
repeat_at: time | None = Body(None),
|
||||
process_after: timedelta | None = Body(None),
|
||||
start_datetime: datetime | None = Body(default=None),
|
||||
end_datetime: datetime | None = Body(default=None),
|
||||
repeat_at: time | None = Body(default=None),
|
||||
process_after: timedelta | None = Body(default=None),
|
||||
):
|
||||
start_process = start_datetime + process_after
|
||||
duration = end_datetime - start_process
|
||||
|
||||
@@ -6,5 +6,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(user_agent: Optional[str] = Header(None)):
|
||||
async def read_items(user_agent: Optional[str] = Header(default=None)):
|
||||
return {"User-Agent": user_agent}
|
||||
|
||||
@@ -4,5 +4,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(user_agent: str | None = Header(None)):
|
||||
async def read_items(user_agent: str | None = Header(default=None)):
|
||||
return {"User-Agent": user_agent}
|
||||
|
||||
@@ -7,6 +7,6 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
strange_header: Optional[str] = Header(None, convert_underscores=False)
|
||||
strange_header: Optional[str] = Header(default=None, convert_underscores=False)
|
||||
):
|
||||
return {"strange_header": strange_header}
|
||||
|
||||
@@ -5,6 +5,6 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
strange_header: str | None = Header(None, convert_underscores=False)
|
||||
strange_header: str | None = Header(default=None, convert_underscores=False)
|
||||
):
|
||||
return {"strange_header": strange_header}
|
||||
|
||||
@@ -6,5 +6,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(x_token: Optional[List[str]] = Header(None)):
|
||||
async def read_items(x_token: Optional[List[str]] = Header(default=None)):
|
||||
return {"X-Token values": x_token}
|
||||
|
||||
@@ -4,5 +4,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(x_token: list[str] | None = Header(None)):
|
||||
async def read_items(x_token: list[str] | None = Header(default=None)):
|
||||
return {"X-Token values": x_token}
|
||||
|
||||
@@ -6,5 +6,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(x_token: Optional[list[str]] = Header(None)):
|
||||
async def read_items(x_token: Optional[list[str]] = Header(default=None)):
|
||||
return {"X-Token values": x_token}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Path, Query
|
||||
|
||||
@@ -7,8 +7,8 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: int = Path(..., title="The ID of the item to get"),
|
||||
q: Optional[str] = Query(None, alias="item-query"),
|
||||
item_id: int = Path(title="The ID of the item to get"),
|
||||
q: Union[str, None] = Query(default=None, alias="item-query"),
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
||||
@@ -5,8 +5,8 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
item_id: int = Path(..., title="The ID of the item to get"),
|
||||
q: str | None = Query(None, alias="item-query"),
|
||||
item_id: int = Path(title="The ID of the item to get"),
|
||||
q: str | None = Query(default=None, alias="item-query"),
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
||||
@@ -4,9 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
q: str, item_id: int = Path(..., title="The ID of the item to get")
|
||||
):
|
||||
async def read_items(q: str, item_id: int = Path(title="The ID of the item to get")):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -4,9 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*, item_id: int = Path(..., title="The ID of the item to get"), q: str
|
||||
):
|
||||
async def read_items(*, item_id: int = Path(title="The ID of the item to get"), q: str):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -5,7 +5,7 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*, item_id: int = Path(..., title="The ID of the item to get", ge=1), q: str
|
||||
*, item_id: int = Path(title="The ID of the item to get", ge=1), q: str
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
||||
@@ -6,7 +6,7 @@ app = FastAPI()
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", gt=0, le=1000),
|
||||
item_id: int = Path(title="The ID of the item to get", gt=0, le=1000),
|
||||
q: str,
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
|
||||
@@ -6,9 +6,9 @@ app = FastAPI()
|
||||
@app.get("/items/{item_id}")
|
||||
async def read_items(
|
||||
*,
|
||||
item_id: int = Path(..., title="The ID of the item to get", ge=0, le=1000),
|
||||
item_id: int = Path(title="The ID of the item to get", ge=0, le=1000),
|
||||
q: str,
|
||||
size: float = Query(..., gt=0, lt=10.5)
|
||||
size: float = Query(gt=0, lt=10.5)
|
||||
):
|
||||
results = {"item_id": item_id}
|
||||
if q:
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -6,7 +6,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[str] = Query(None, max_length=50)):
|
||||
async def read_items(q: Union[str, None] = Query(default=None, max_length=50)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -4,7 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, max_length=50)):
|
||||
async def read_items(q: str | None = Query(default=None, max_length=50)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -6,7 +6,9 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[str] = Query(None, min_length=3, max_length=50)):
|
||||
async def read_items(
|
||||
q: Union[str, None] = Query(default=None, min_length=3, max_length=50)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -4,7 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, min_length=3, max_length=50)):
|
||||
async def read_items(q: str | None = Query(default=None, min_length=3, max_length=50)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -7,7 +7,9 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(None, min_length=3, max_length=50, regex="^fixedquery$")
|
||||
q: Union[str, None] = Query(
|
||||
default=None, min_length=3, max_length=50, regex="^fixedquery$"
|
||||
)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
|
||||
@@ -5,7 +5,8 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: str | None = Query(None, min_length=3, max_length=50, regex="^fixedquery$")
|
||||
q: str
|
||||
| None = Query(default=None, min_length=3, max_length=50, regex="^fixedquery$")
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
|
||||
@@ -4,7 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query("fixedquery", min_length=3)):
|
||||
async def read_items(q: str = Query(default="fixedquery", min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -4,7 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query(..., min_length=3)):
|
||||
async def read_items(q: str = Query(min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
11
docs_src/query_params_str_validations/tutorial006b.py
Normal file
11
docs_src/query_params_str_validations/tutorial006b.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query(default=..., min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
13
docs_src/query_params_str_validations/tutorial006c.py
Normal file
13
docs_src/query_params_str_validations/tutorial006c.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Union[str, None] = Query(default=..., min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
11
docs_src/query_params_str_validations/tutorial006c_py310.py
Normal file
11
docs_src/query_params_str_validations/tutorial006c_py310.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(default=..., min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
12
docs_src/query_params_str_validations/tutorial006d.py
Normal file
12
docs_src/query_params_str_validations/tutorial006d.py
Normal file
@@ -0,0 +1,12 @@
|
||||
from fastapi import FastAPI, Query
|
||||
from pydantic import Required
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str = Query(default=Required, min_length=3)):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
return results
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -7,7 +7,7 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(None, title="Query string", min_length=3)
|
||||
q: Union[str, None] = Query(default=None, title="Query string", min_length=3)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
|
||||
@@ -4,7 +4,9 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, title="Query string", min_length=3)):
|
||||
async def read_items(
|
||||
q: str | None = Query(default=None, title="Query string", min_length=3)
|
||||
):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -7,8 +7,8 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(
|
||||
None,
|
||||
q: Union[str, None] = Query(
|
||||
default=None,
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
min_length=3,
|
||||
|
||||
@@ -7,7 +7,7 @@ app = FastAPI()
|
||||
async def read_items(
|
||||
q: str
|
||||
| None = Query(
|
||||
None,
|
||||
default=None,
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
min_length=3,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -6,7 +6,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[str] = Query(None, alias="item-query")):
|
||||
async def read_items(q: Union[str, None] = Query(default=None, alias="item-query")):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -4,7 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: str | None = Query(None, alias="item-query")):
|
||||
async def read_items(q: str | None = Query(default=None, alias="item-query")):
|
||||
results = {"items": [{"item_id": "Foo"}, {"item_id": "Bar"}]}
|
||||
if q:
|
||||
results.update({"q": q})
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -7,8 +7,8 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
q: Optional[str] = Query(
|
||||
None,
|
||||
q: Union[str, None] = Query(
|
||||
default=None,
|
||||
alias="item-query",
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
|
||||
@@ -7,7 +7,7 @@ app = FastAPI()
|
||||
async def read_items(
|
||||
q: str
|
||||
| None = Query(
|
||||
None,
|
||||
default=None,
|
||||
alias="item-query",
|
||||
title="Query string",
|
||||
description="Query string for the items to search in the database that have a good match",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import List, Optional
|
||||
from typing import List, Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -6,6 +6,6 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[List[str]] = Query(None)):
|
||||
async def read_items(q: Union[List[str], None] = Query(default=None)):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
||||
@@ -4,6 +4,6 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: list[str] | None = Query(None)):
|
||||
async def read_items(q: list[str] | None = Query(default=None)):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -6,6 +6,6 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: Optional[list[str]] = Query(None)):
|
||||
async def read_items(q: Union[list[str], None] = Query(default=None)):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
||||
@@ -6,6 +6,6 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: List[str] = Query(["foo", "bar"])):
|
||||
async def read_items(q: List[str] = Query(default=["foo", "bar"])):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
||||
@@ -4,6 +4,6 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: list[str] = Query(["foo", "bar"])):
|
||||
async def read_items(q: list[str] = Query(default=["foo", "bar"])):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
||||
@@ -4,6 +4,6 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(q: list = Query([])):
|
||||
async def read_items(q: list = Query(default=[])):
|
||||
query_items = {"q": q}
|
||||
return query_items
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from typing import Optional
|
||||
from typing import Union
|
||||
|
||||
from fastapi import FastAPI, Query
|
||||
|
||||
@@ -7,7 +7,7 @@ app = FastAPI()
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(
|
||||
hidden_query: Optional[str] = Query(None, include_in_schema=False)
|
||||
hidden_query: Union[str, None] = Query(default=None, include_in_schema=False)
|
||||
):
|
||||
if hidden_query:
|
||||
return {"hidden_query": hidden_query}
|
||||
|
||||
@@ -4,7 +4,9 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items(hidden_query: str | None = Query(None, include_in_schema=False)):
|
||||
async def read_items(
|
||||
hidden_query: str | None = Query(default=None, include_in_schema=False)
|
||||
):
|
||||
if hidden_query:
|
||||
return {"hidden_query": hidden_query}
|
||||
else:
|
||||
|
||||
@@ -4,7 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: bytes = File(...)):
|
||||
async def create_file(file: bytes = File()):
|
||||
return {"file_size": len(file)}
|
||||
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: Optional[bytes] = File(None)):
|
||||
async def create_file(file: Optional[bytes] = File(default=None)):
|
||||
if not file:
|
||||
return {"message": "No file sent"}
|
||||
else:
|
||||
|
||||
@@ -4,7 +4,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: bytes | None = File(None)):
|
||||
async def create_file(file: bytes | None = File(default=None)):
|
||||
if not file:
|
||||
return {"message": "No file sent"}
|
||||
else:
|
||||
|
||||
@@ -4,12 +4,12 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(file: bytes = File(..., description="A file read as bytes")):
|
||||
async def create_file(file: bytes = File(description="A file read as bytes")):
|
||||
return {"file_size": len(file)}
|
||||
|
||||
|
||||
@app.post("/uploadfile/")
|
||||
async def create_upload_file(
|
||||
file: UploadFile = File(..., description="A file read as UploadFile")
|
||||
file: UploadFile = File(description="A file read as UploadFile"),
|
||||
):
|
||||
return {"filename": file.filename}
|
||||
|
||||
@@ -7,7 +7,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_files(files: List[bytes] = File(...)):
|
||||
async def create_files(files: List[bytes] = File()):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_files(files: list[bytes] = File(...)):
|
||||
async def create_files(files: list[bytes] = File()):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
|
||||
@@ -8,14 +8,14 @@ app = FastAPI()
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_files(
|
||||
files: List[bytes] = File(..., description="Multiple files as bytes")
|
||||
files: List[bytes] = File(description="Multiple files as bytes"),
|
||||
):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
@app.post("/uploadfiles/")
|
||||
async def create_upload_files(
|
||||
files: List[UploadFile] = File(..., description="Multiple files as UploadFile")
|
||||
files: List[UploadFile] = File(description="Multiple files as UploadFile"),
|
||||
):
|
||||
return {"filenames": [file.filename for file in files]}
|
||||
|
||||
|
||||
@@ -6,14 +6,14 @@ app = FastAPI()
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_files(
|
||||
files: list[bytes] = File(..., description="Multiple files as bytes")
|
||||
files: list[bytes] = File(description="Multiple files as bytes"),
|
||||
):
|
||||
return {"file_sizes": [len(file) for file in files]}
|
||||
|
||||
|
||||
@app.post("/uploadfiles/")
|
||||
async def create_upload_files(
|
||||
files: list[UploadFile] = File(..., description="Multiple files as UploadFile")
|
||||
files: list[UploadFile] = File(description="Multiple files as UploadFile"),
|
||||
):
|
||||
return {"filenames": [file.filename for file in files]}
|
||||
|
||||
|
||||
@@ -4,5 +4,5 @@ app = FastAPI()
|
||||
|
||||
|
||||
@app.post("/login/")
|
||||
async def login(username: str = Form(...), password: str = Form(...)):
|
||||
async def login(username: str = Form(), password: str = Form()):
|
||||
return {"username": username}
|
||||
|
||||
@@ -5,7 +5,7 @@ app = FastAPI()
|
||||
|
||||
@app.post("/files/")
|
||||
async def create_file(
|
||||
file: bytes = File(...), fileb: UploadFile = File(...), token: str = Form(...)
|
||||
file: bytes = File(), fileb: UploadFile = File(), token: str = Form()
|
||||
):
|
||||
return {
|
||||
"file_size": len(file),
|
||||
|
||||
@@ -7,10 +7,10 @@ app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str = Field(..., example="Foo")
|
||||
description: Optional[str] = Field(None, example="A very nice Item")
|
||||
price: float = Field(..., example=35.4)
|
||||
tax: Optional[float] = Field(None, example=3.2)
|
||||
name: str = Field(example="Foo")
|
||||
description: Optional[str] = Field(default=None, example="A very nice Item")
|
||||
price: float = Field(example=35.4)
|
||||
tax: Optional[float] = Field(default=None, example=3.2)
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
|
||||
@@ -5,10 +5,10 @@ app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str = Field(..., example="Foo")
|
||||
description: str | None = Field(None, example="A very nice Item")
|
||||
price: float = Field(..., example=35.4)
|
||||
tax: float | None = Field(None, example=3.2)
|
||||
name: str = Field(example="Foo")
|
||||
description: str | None = Field(default=None, example="A very nice Item")
|
||||
price: float = Field(example=35.4)
|
||||
tax: float | None = Field(default=None, example=3.2)
|
||||
|
||||
|
||||
@app.put("/items/{item_id}")
|
||||
|
||||
@@ -17,7 +17,6 @@ class Item(BaseModel):
|
||||
async def update_item(
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
example={
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
|
||||
@@ -15,7 +15,6 @@ class Item(BaseModel):
|
||||
async def update_item(
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
example={
|
||||
"name": "Foo",
|
||||
"description": "A very nice Item",
|
||||
|
||||
@@ -18,7 +18,6 @@ async def update_item(
|
||||
*,
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
examples={
|
||||
"normal": {
|
||||
"summary": "A normal example",
|
||||
|
||||
@@ -16,7 +16,6 @@ async def update_item(
|
||||
*,
|
||||
item_id: int,
|
||||
item: Item = Body(
|
||||
...,
|
||||
examples={
|
||||
"normal": {
|
||||
"summary": "A normal example",
|
||||
|
||||
@@ -57,8 +57,8 @@ async def get():
|
||||
|
||||
async def get_cookie_or_token(
|
||||
websocket: WebSocket,
|
||||
session: Optional[str] = Cookie(None),
|
||||
token: Optional[str] = Query(None),
|
||||
session: Optional[str] = Cookie(default=None),
|
||||
token: Optional[str] = Query(default=None),
|
||||
):
|
||||
if session is None and token is None:
|
||||
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
|
||||
|
||||
Reference in New Issue
Block a user