mirror of
https://github.com/fastapi/fastapi.git
synced 2026-10-25 19:29:58 -04:00
✨ Add support for dataclasses in request bodies and response_model (#3577)
This commit is contained in:
1 parent
75d8a6b5c2
commit
684c0077cc
10 files changed
+562
No files matched your search
@@ -0,0 +1,26 @@
|
||||
from dataclasses import dataclass, field
|
||||
from typing import List, Optional
|
||||
|
||||
from fastapi import FastAPI
|
||||
|
||||
|
||||
@dataclass
|
||||
class Item:
|
||||
name: str
|
||||
price: float
|
||||
tags: List[str] = field(default_factory=list)
|
||||
description: Optional[str] = None
|
||||
tax: Optional[float] = None
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get("/items/next", response_model=Item)
|
||||
async def read_next_item():
|
||||
return {
|
||||
"name": "Island In The Moon",
|
||||
"price": 12.99,
|
||||
"description": "A place to be be playin' and havin' fun",
|
||||
"tags": ["breater"],
|
||||
}
|
||||
Reference in new issue
Block a user