From e4d408cb96c030c11450b889bc910ba5e79bcfdd Mon Sep 17 00:00:00 2001 From: ivan_kirpichnikov Date: Thu, 27 Aug 2026 16:01:09 +0300 Subject: [PATCH] bugfix: fix inject FastAPI in lifespan --- src/faststream_fastapi/faststream_api.py | 2 ++ tests/base/in_memory.py | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/faststream_fastapi/faststream_api.py b/src/faststream_fastapi/faststream_api.py index 8dc4eda..1899bc4 100644 --- a/src/faststream_fastapi/faststream_api.py +++ b/src/faststream_fastapi/faststream_api.py @@ -107,6 +107,8 @@ def context(self) -> ContextRepo: return self._startable_application.context async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: + scope["app"] = self._application + if scope["type"] == "lifespan": await self.lifespan(scope, receive, send) return None diff --git a/tests/base/in_memory.py b/tests/base/in_memory.py index 89b81c1..25c2c5e 100644 --- a/tests/base/in_memory.py +++ b/tests/base/in_memory.py @@ -407,6 +407,23 @@ async def handler(request: Request) -> None: mock.start.assert_called_once() mock.close.assert_called_once() + async def test_lifespan_app(self, mock: Mock) -> None: + @asynccontextmanager + async def lifespan(application: FastAPI) -> AsyncIterator[None]: + mock(application) + yield None + + application = FastAPI(lifespan=lifespan) + broker = self.get_broker() + + async with ( + self.patch_broker(broker), + self.get_test_client(broker, application=application), + ): + pass + + mock.assert_called_once_with(application) + async def test_subscriber_mock(self, queue: str) -> None: broker = self.get_broker()