mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-25 07:08:11 -05:00
* 📝 Add docs recommending Union over Optional * 📝 Update docs recommending Union over Optional * 📝 Update source examples for docs, recommend Union over Optional * 📝 Update highlighted lines with updated source examples * 📝 Update highlighted lines in Markdown with recent code changes * 📝 Update docs, use Union instead of Optional * ♻️ Update source examples to recommend Union over Optional * 🎨 Update highlighted code in Markdown after moving from Optional to Union
24 lines
486 B
Python
24 lines
486 B
Python
from typing import Union
|
|
|
|
from fastapi import Cookie, Depends, FastAPI
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
def query_extractor(q: Union[str, None] = None):
|
|
return q
|
|
|
|
|
|
def query_or_cookie_extractor(
|
|
q: str = Depends(query_extractor),
|
|
last_query: Union[str, None] = Cookie(default=None),
|
|
):
|
|
if not q:
|
|
return last_query
|
|
return q
|
|
|
|
|
|
@app.get("/items/")
|
|
async def read_query(query_or_default: str = Depends(query_or_cookie_extractor)):
|
|
return {"q_or_cookie": query_or_default}
|