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
2022-05-13 18:38:22 -05:00
committed by GitHub
parent 31690dda2c
commit 9262fa8362
107 changed files with 404 additions and 314 deletions

View File

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

View File

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