Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail to load openapi.json when mounting API as sub application #64

Open
beenje opened this issue Feb 19, 2025 · 0 comments
Open

Fail to load openapi.json when mounting API as sub application #64

beenje opened this issue Feb 19, 2025 · 0 comments

Comments

@beenje
Copy link

beenje commented Feb 19, 2025

I'd like to mount my versioned API under /api.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant