Skip to content

Instantly share code, notes, and snippets.

@Kylmakalle
Last active October 19, 2020 20:03
Show Gist options
  • Save Kylmakalle/8682416c2155b99a89961d33c0d3988b to your computer and use it in GitHub Desktop.
Save Kylmakalle/8682416c2155b99a89961d33c0d3988b to your computer and use it in GitHub Desktop.
from fastapi import FastAPI
app = FastAPI()
from typing import Optional
from uvicorn import run
from pydantic import BaseModel, Field
class AddressResponse1(BaseModel):
city: Optional[str] = Field(..., nullable=True, example="London")
street: Optional[str] = Field(..., nullable=True, example="Baker street")
house: Optional[str] = Field(..., nullable=True, example="221")
block: Optional[str] = Field(..., nullable=True, example=None)
@app.get('v1/address', response_model=AddressResponse1)
async def get_address_v1():
return AddressResponse1(city='London', street='Baker street', house='221', block=None)
class AddressResponse2(BaseModel):
city: Optional[str] = Field(..., nullable=True)
street: Optional[str] = Field(..., nullable=True)
house: Optional[str] = Field(..., nullable=True)
block: Optional[str] = Field(..., nullable=True)
class Config:
schema_extra = {
"example": {
"city": "London",
"street": "Baker street",
"house": "221",
"block": None
}
}
@app.get('v2/address', response_model=AddressResponse2)
async def get_address_v2():
return AddressResponse2(city='London', street='Baker street', house='221', block=None)
run(app, debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment