You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
from typing import List
from fastapi import FastAPI, APIRouter
from pydantic import BaseModel
from fastapi_versionizer.versionizer import Versionizer, api_version
class User(BaseModel):
id: int
name: str
class UserV2(BaseModel):
id: int
name: str
age: int
db = {
'users': {}
}
app = FastAPI(
title='test',
redoc_url=None
)
users_router = APIRouter(
prefix='/users',
tags=['Users']
)
@app.get('/status', tags=['Status'])
def get_status() -> str:
return 'Ok'
@api_version(1)
@users_router.get('', deprecated=True)
def get_users() -> List[User]:
return list(user for user in db['users'].values() if isinstance(user, User))
@api_version(1)
@users_router.post('', deprecated=True)
def create_user(user: User) -> User:
db['users'][user.id] = user
return user
@api_version(2)
@users_router.get('')
def get_users_v2() -> List[UserV2]:
return list(user for user in db['users'].values() if isinstance(user, UserV2))
@api_version(2)
@users_router.post('')
def create_user_v2(user: UserV2) -> UserV2:
db['users'][user.id] = user
return user
# Sub application for the API
original_api = FastAPI()
original_api.include_router(users_router)
app.mount("/api", original_api)
versions = Versionizer(
app=original_api,
prefix_format='/v{major}',
semantic_version_format='{major}',
latest_prefix='/latest',
sort_routes=True
).versionize()
With this example:
/docs works and shows GET /status as expected.
/api/docs works and shows both v1 and v2 endpoints
/api/v2/docs returns Failed to load API definition. It's trying to load /v2/openapi.json instead of /api/v2/openapi.json.
The text was updated successfully, but these errors were encountered:
I'd like to mount my versioned API under
/api
.With this example:
/docs
works and showsGET /status
as expected./api/docs
works and shows both v1 and v2 endpoints/api/v2/docs
returnsFailed to load API definition
. It's trying to load/v2/openapi.json
instead of/api/v2/openapi.json
.The text was updated successfully, but these errors were encountered: