Last active
July 25, 2024 14:13
-
-
Save SF-300/7877b73e32e285f3ceae57b93aa2b734 to your computer and use it in GitHub Desktop.
Mounting ASGI subapps through FastAPI's router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest.mock | |
from fastapi import FastAPI as _FastAPI, APIRouter as _APIRouter, routing | |
__all__ = "FastAPI", "APIRouter" | |
class APIRouter(_APIRouter): | |
def include_router(self, router, **kwargs) -> None: | |
prefix = kwargs.get("prefix", "") | |
super().include_router(router, **kwargs) | |
# NOTE(zeronineseven): FastAPI does not handle ASGI subapps mounted to routers - they get simply ignored when | |
# downstream router's routes get copied into the upstream. So this is a simple hack to | |
# actually copy them too. | |
for mount in (r for r in router.routes if isinstance(r, routing.Mount)): | |
self.mount(prefix + mount.path, mount.app, mount.name) | |
class FastAPI(_FastAPI): | |
def __init__(self, *args, **kwargs): | |
called = False | |
def create_router(*args_, **kwargs_): | |
nonlocal called | |
called = True | |
return APIRouter(*args_, **kwargs_) | |
with unittest.mock.patch("fastapi.routing.APIRouter", create_router): | |
super().__init__(*args, **kwargs) | |
if not called: | |
raise AssertionError("Patching failed! Seems like something has changed in recent FastAPI versions...") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A (rather hack-ish) workaround for this problem.