✨ 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:
Sebastián Ramírez authored and GitHub committed 2022-05-13 18:38:22 -05:00
1 parent 31690dda2c
commit 9262fa8362
107 files changed
+404 -314

No files matched your search

@@ -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: