cadwyn: `app.generate_and_include_versioned_routers` does not register routes

Describe the bug

When calling app.generate_and_include_versioned_routers in Cadwyn 4.4.0 and beyond, it no longer registers the routes automatically. The same code (below) works for version 4.3.1.

To Reproduce

  1. Create a main.py file with the following content:
from cadwyn import Cadwyn, HeadVersion, Version, VersionBundle, VersionedAPIRouter

app = Cadwyn(versions=VersionBundle(HeadVersion(), Version("2000-01-01")))

router = VersionedAPIRouter()

@router.get("/")
async def root():
    return {"message": "Hello World"}

app.generate_and_include_versioned_routers(router)

for version, router in app.router.versioned_routers.items():
    print("Version:", version)
    print("Routes:", router.routes)
  1. Up on running this file via fastapi dev main.py, it prints empty routes.
Version: 2000-01-01
Routes: []

Expected behavior

I would expect this to print the routes it printed before 4.4.0

Version: 2000-01-01
Routes: [Route(path='/openapi.json', name='openapi_jsons', methods=['GET', 'HEAD']), APIRoute(path='/', name='root', methods=['GET'])]

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 28 (11 by maintainers)

Most upvoted comments

Hm… Then my strategy would probably be to use two different Cadwyn apps in such case

Understood. Then your best bet would be calling lifespan through a client (either an httpx.AsyncClient or a fastapi.testclient.TestClient):

from fastapi.openapi.utils import get_openapi
from httpx import AsyncClient
from fastapi.testclient import TestClient


with TestClient(app=app):
    for version, router in app.router.versioned_routers.items():
        openapi = get_openapi(..., routes=router.routes, ...)

This interface is public (unlike the one you use right now) and will be kept in the future versions of both Cadwyn and FastAPI.