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
26 lines
656 B
Python
26 lines
656 B
Python
from typing import Union
|
|
|
|
from fastapi import Depends, FastAPI
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
fake_items_db = [{"item_name": "Foo"}, {"item_name": "Bar"}, {"item_name": "Baz"}]
|
|
|
|
|
|
class CommonQueryParams:
|
|
def __init__(self, q: Union[str, None] = None, skip: int = 0, limit: int = 100):
|
|
self.q = q
|
|
self.skip = skip
|
|
self.limit = limit
|
|
|
|
|
|
@app.get("/items/")
|
|
async def read_items(commons: CommonQueryParams = Depends(CommonQueryParams)):
|
|
response = {}
|
|
if commons.q:
|
|
response.update({"q": commons.q})
|
|
items = fake_items_db[commons.skip : commons.skip + commons.limit]
|
|
response.update({"items": items})
|
|
return response
|