mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-14 02:35:51 -04:00
✨ Add support for function return type annotations to declare the response_model (#1436)
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
This commit is contained in:
27
docs_src/response_model/tutorial001_01.py
Normal file
27
docs_src/response_model/tutorial001_01.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from typing import List, Union
|
||||
|
||||
from fastapi import FastAPI
|
||||
from pydantic import BaseModel
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
name: str
|
||||
description: Union[str, None] = None
|
||||
price: float
|
||||
tax: Union[float, None] = None
|
||||
tags: List[str] = []
|
||||
|
||||
|
||||
@app.post("/items/")
|
||||
async def create_item(item: Item) -> Item:
|
||||
return item
|
||||
|
||||
|
||||
@app.get("/items/")
|
||||
async def read_items() -> List[Item]:
|
||||
return [
|
||||
Item(name="Portal Gun", price=42.0),
|
||||
Item(name="Plumbus", price=32.0),
|
||||
]
|
||||
Reference in New Issue
Block a user