FastAPI and OpenAPI with Union type and oneOf

# .python-version # 3.12.0 # # requirements.txt # fastapi[standard]==0.114.0 # # main.py from typing import Literal, Union from pydantic import BaseModel, Field from fastapi import FastAPI app = FastAPI() class Cat(BaseModel): pet_type: Literal["c"] meows: int class Dog(BaseModel): pet_type: Literal["d"] barks: float class Pet(BaseModel): pet: Union[Cat, Dog] = Field(..., discriminator="pet_type") age: int @app.post("/cat") def post_cat(pet: Cat): return {"pet": pet} @app.post("/dog") def post_dog(pet: Dog): return {"pet": pet} @app.post("/pet") def post_pet(pet: Pet): return {"pet": pet}

September 8, 2024 · 1 min · Ask Blaker

Heroku docker FastAPI

Make sure you have the Heroku CLI and GIT See this repo or the spoiler below: git clone git@github.com:askblaker/fastapi-docker-heroku.git cd fastapi-docker-heroku heroku create <your-app-name> heroku git:remote <your-app-name> heroku stack:set container git push heroku main Enjoy your api at https://your-app-name.herokuapp.com

November 3, 2020 · 1 min · Ask Blaker