mirror of
https://github.com/fastapi/fastapi.git
synced 2025-12-23 22:29:34 -05:00
Co-authored-by: Yurii Motov <yurii.motov.monte@gmail.com> Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
36 lines
644 B
Python
36 lines
644 B
Python
from typing import Union
|
|
|
|
from fastapi import FastAPI
|
|
from pydantic import BaseModel
|
|
|
|
app = FastAPI()
|
|
|
|
|
|
class BaseItem(BaseModel):
|
|
description: str
|
|
type: str
|
|
|
|
|
|
class CarItem(BaseItem):
|
|
type: str = "car"
|
|
|
|
|
|
class PlaneItem(BaseItem):
|
|
type: str = "plane"
|
|
size: int
|
|
|
|
|
|
items = {
|
|
"item1": {"description": "All my friends drive a low rider", "type": "car"},
|
|
"item2": {
|
|
"description": "Music is my aeroplane, it's my aeroplane",
|
|
"type": "plane",
|
|
"size": 5,
|
|
},
|
|
}
|
|
|
|
|
|
@app.get("/items/{item_id}", response_model=Union[PlaneItem, CarItem])
|
|
async def read_item(item_id: str):
|
|
return items[item_id]
|