mirror of https://github.com/tiangolo/fastapi.git
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]
|