diff --git a/README.md b/README.md index bccaa4a10e..b9b44ca300 100644 --- a/README.md +++ b/README.md @@ -318,7 +318,7 @@ class Incoming(BaseModel): async def hello(m: Incoming): return {"response": "Hello, world!"} -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) ``` diff --git a/docs/create_api_docs.py b/docs/create_api_docs.py index da84276487..64dafda5ff 100644 --- a/docs/create_api_docs.py +++ b/docs/create_api_docs.py @@ -346,7 +346,7 @@ def create_api_docs( def on_page_markdown(markdown, *, page, config, files): """Mkdocs hook to update the edit URL for the public API pages.""" - if "public_api" in page.edit_url: + if page.edit_url and "public_api" in page.edit_url: page.edit_url = page.edit_url.replace("public_api", "api") diff --git a/docs/docs/en/getting-started/integrations/fastapi/index.md b/docs/docs/en/getting-started/integrations/fastapi/index.md index 5587fcc7e3..814b144bdb 100644 --- a/docs/docs/en/getting-started/integrations/fastapi/index.md +++ b/docs/docs/en/getting-started/integrations/fastapi/index.md @@ -26,6 +26,9 @@ Just import a **StreamRouter** you need and declare the message handler in the s {! includes/getting_started/integrations/fastapi/1.md !} +!!! warning + If you are using **fastapi < 0.102.2** version, you should setup lifespan manually `#!python FastAPI(lifespan=router.lifespan_context)` + When processing a message from a broker, the entire message body is placed simultaneously in both the `body` and `path` request parameters. You can access them in any way convenient for you. The message header is placed in `headers`. Also, this router can be fully used as an `HttpRouter` (of which it is the inheritor). So, you can @@ -100,7 +103,7 @@ This way the core router collects all nested routers publishers and subscribers ### Custom lifespan -Otherwise, if you want to has multiple connections to different broker instances, you should start routers independently in your custom lifespan +Otherwise, if you want to has multiple connections to different broker instances, you can just include them separately to the app (each router has own broker and connection in this case) {! includes/getting_started/integrations/fastapi/multiple_lifespan.md !} diff --git a/docs/docs/en/release.md b/docs/docs/en/release.md index 09850cf176..e019b025cc 100644 --- a/docs/docs/en/release.md +++ b/docs/docs/en/release.md @@ -18,7 +18,7 @@ hide: The current release is planned as a latest feature release before **0.6.0**. All other **0.5.19+** releases will contain only minor bugfixes and all the team work will be focused on next major one. -There a lot of changes we want to present you now though! +There's a lot of changes we want to present you now though! #### New RPC feature diff --git a/docs/docs_src/integrations/fastapi/confluent/base.py b/docs/docs_src/integrations/fastapi/confluent/base.py index 8808d98717..e73d087f63 100644 --- a/docs/docs_src/integrations/fastapi/confluent/base.py +++ b/docs/docs_src/integrations/fastapi/confluent/base.py @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)): async def hello_http(): return "Hello, HTTP!" -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/confluent/depends.py b/docs/docs_src/integrations/fastapi/confluent/depends.py index 1d05fcbd3c..e728c5acde 100644 --- a/docs/docs_src/integrations/fastapi/confluent/depends.py +++ b/docs/docs_src/integrations/fastapi/confluent/depends.py @@ -5,7 +5,7 @@ router = fastapi.KafkaRouter("localhost:9092") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() def broker(): diff --git a/docs/docs_src/integrations/fastapi/confluent/multiple.py b/docs/docs_src/integrations/fastapi/confluent/multiple.py index 511463bd59..ae65011564 100644 --- a/docs/docs_src/integrations/fastapi/confluent/multiple.py +++ b/docs/docs_src/integrations/fastapi/confluent/multiple.py @@ -14,5 +14,5 @@ async def nested_handler(): core_router.include_router(nested_router) -app = FastAPI(lifespan=core_router.lifespan_context) +app = FastAPI() app.include_router(core_router) diff --git a/docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py index 068bc5ef7a..d4fb359d5b 100644 --- a/docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py +++ b/docs/docs_src/integrations/fastapi/confluent/multiple_lifespan.py @@ -1,19 +1,11 @@ -from contextlib import asynccontextmanager - from fastapi import FastAPI from faststream.confluent.fastapi import KafkaRouter -core_router = KafkaRouter() -nested_router = KafkaRouter() +one_router = KafkaRouter() +another_router = KafkaRouter() -@asynccontextmanager -async def lifespan(app: FastAPI): - async with ( - core_router.lifespan_context(app), - nested_router.lifespan_context(app), - ): - yield +... -app = FastAPI(lifespan=lifespan) -app.include_router(core_router) -app.include_router(nested_router) +app = FastAPI() +app.include_router(one_router) +app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/confluent/send.py b/docs/docs_src/integrations/fastapi/confluent/send.py index 6e4632a5cd..b01a42d134 100644 --- a/docs/docs_src/integrations/fastapi/confluent/send.py +++ b/docs/docs_src/integrations/fastapi/confluent/send.py @@ -4,7 +4,7 @@ router = KafkaRouter("localhost:9092") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() @router.get("/") diff --git a/docs/docs_src/integrations/fastapi/confluent/startup.py b/docs/docs_src/integrations/fastapi/confluent/startup.py index 12b8501c70..f6f819606d 100644 --- a/docs/docs_src/integrations/fastapi/confluent/startup.py +++ b/docs/docs_src/integrations/fastapi/confluent/startup.py @@ -15,5 +15,5 @@ async def test(app: FastAPI): await router.broker.publish("Hello!", "test") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/kafka/base.py b/docs/docs_src/integrations/fastapi/kafka/base.py index 6ceffcb6dd..1930ce006c 100644 --- a/docs/docs_src/integrations/fastapi/kafka/base.py +++ b/docs/docs_src/integrations/fastapi/kafka/base.py @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)): async def hello_http(): return "Hello, HTTP!" -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/kafka/depends.py b/docs/docs_src/integrations/fastapi/kafka/depends.py index 5fd837e1d7..a89d28a4e5 100644 --- a/docs/docs_src/integrations/fastapi/kafka/depends.py +++ b/docs/docs_src/integrations/fastapi/kafka/depends.py @@ -5,7 +5,7 @@ router = fastapi.KafkaRouter("localhost:9092") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() def broker(): diff --git a/docs/docs_src/integrations/fastapi/kafka/multiple.py b/docs/docs_src/integrations/fastapi/kafka/multiple.py index d13d6627b0..6bd9f8b94c 100644 --- a/docs/docs_src/integrations/fastapi/kafka/multiple.py +++ b/docs/docs_src/integrations/fastapi/kafka/multiple.py @@ -14,5 +14,5 @@ async def nested_handler(): core_router.include_router(nested_router) -app = FastAPI(lifespan=core_router.lifespan_context) +app = FastAPI() app.include_router(core_router) diff --git a/docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py index 4b3ff2ea82..abf30d352c 100644 --- a/docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py +++ b/docs/docs_src/integrations/fastapi/kafka/multiple_lifespan.py @@ -1,19 +1,11 @@ -from contextlib import asynccontextmanager - from fastapi import FastAPI from faststream.kafka.fastapi import KafkaRouter -core_router = KafkaRouter() -nested_router = KafkaRouter() +one_router = KafkaRouter() +another_router = KafkaRouter() -@asynccontextmanager -async def lifespan(app: FastAPI): - async with ( - core_router.lifespan_context(app), - nested_router.lifespan_context(app), - ): - yield +... -app = FastAPI(lifespan=lifespan) -app.include_router(core_router) -app.include_router(nested_router) +app = FastAPI() +app.include_router(one_router) +app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/kafka/send.py b/docs/docs_src/integrations/fastapi/kafka/send.py index d7a0cea50f..32dd3f9c37 100644 --- a/docs/docs_src/integrations/fastapi/kafka/send.py +++ b/docs/docs_src/integrations/fastapi/kafka/send.py @@ -4,7 +4,7 @@ router = KafkaRouter("localhost:9092") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() @router.get("/") diff --git a/docs/docs_src/integrations/fastapi/kafka/startup.py b/docs/docs_src/integrations/fastapi/kafka/startup.py index 7f802f1356..6f34c61089 100644 --- a/docs/docs_src/integrations/fastapi/kafka/startup.py +++ b/docs/docs_src/integrations/fastapi/kafka/startup.py @@ -15,5 +15,5 @@ async def test(app: FastAPI): await router.broker.publish("Hello!", "test") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/nats/base.py b/docs/docs_src/integrations/fastapi/nats/base.py index 6f99918275..03b8b2b733 100644 --- a/docs/docs_src/integrations/fastapi/nats/base.py +++ b/docs/docs_src/integrations/fastapi/nats/base.py @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)): async def hello_http(): return "Hello, HTTP!" -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/nats/depends.py b/docs/docs_src/integrations/fastapi/nats/depends.py index 4eec1505f7..0f1784f19e 100644 --- a/docs/docs_src/integrations/fastapi/nats/depends.py +++ b/docs/docs_src/integrations/fastapi/nats/depends.py @@ -5,7 +5,7 @@ router = fastapi.NatsRouter("nats://localhost:4222") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() def broker(): diff --git a/docs/docs_src/integrations/fastapi/nats/multiple.py b/docs/docs_src/integrations/fastapi/nats/multiple.py index f39f1546e0..6743e6fbcf 100644 --- a/docs/docs_src/integrations/fastapi/nats/multiple.py +++ b/docs/docs_src/integrations/fastapi/nats/multiple.py @@ -14,5 +14,5 @@ async def nested_handler(): core_router.include_router(nested_router) -app = FastAPI(lifespan=core_router.lifespan_context) +app = FastAPI() app.include_router(core_router) diff --git a/docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py index 25076efcc3..7ea737ca09 100644 --- a/docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py +++ b/docs/docs_src/integrations/fastapi/nats/multiple_lifespan.py @@ -1,19 +1,11 @@ -from contextlib import asynccontextmanager - from fastapi import FastAPI from faststream.nats.fastapi import NatsRouter -core_router = NatsRouter() -nested_router = NatsRouter() +one_router = NatsRouter() +another_router = NatsRouter() -@asynccontextmanager -async def lifespan(app: FastAPI): - async with ( - core_router.lifespan_context(app), - nested_router.lifespan_context(app), - ): - yield +... -app = FastAPI(lifespan=lifespan) -app.include_router(core_router) -app.include_router(nested_router) +app = FastAPI() +app.include_router(one_router) +app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/nats/send.py b/docs/docs_src/integrations/fastapi/nats/send.py index e2d0f90ad2..4a1413d113 100644 --- a/docs/docs_src/integrations/fastapi/nats/send.py +++ b/docs/docs_src/integrations/fastapi/nats/send.py @@ -4,7 +4,7 @@ router = NatsRouter("nats://localhost:4222") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() @router.get("/") diff --git a/docs/docs_src/integrations/fastapi/nats/startup.py b/docs/docs_src/integrations/fastapi/nats/startup.py index 3131b03d22..4a2530afe3 100644 --- a/docs/docs_src/integrations/fastapi/nats/startup.py +++ b/docs/docs_src/integrations/fastapi/nats/startup.py @@ -15,5 +15,5 @@ async def test(app: FastAPI): await router.broker.publish("Hello!", "test") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/rabbit/base.py b/docs/docs_src/integrations/fastapi/rabbit/base.py index 97b48f99a0..d9b0f0b741 100644 --- a/docs/docs_src/integrations/fastapi/rabbit/base.py +++ b/docs/docs_src/integrations/fastapi/rabbit/base.py @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)): async def hello_http(): return "Hello, HTTP!" -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/rabbit/depends.py b/docs/docs_src/integrations/fastapi/rabbit/depends.py index 6dfb0074f1..840f9e32b3 100644 --- a/docs/docs_src/integrations/fastapi/rabbit/depends.py +++ b/docs/docs_src/integrations/fastapi/rabbit/depends.py @@ -5,7 +5,7 @@ router = fastapi.RabbitRouter("amqp://guest:guest@localhost:5672/") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() def broker(): diff --git a/docs/docs_src/integrations/fastapi/rabbit/multiple.py b/docs/docs_src/integrations/fastapi/rabbit/multiple.py index 6e7571537b..f7b3b37d48 100644 --- a/docs/docs_src/integrations/fastapi/rabbit/multiple.py +++ b/docs/docs_src/integrations/fastapi/rabbit/multiple.py @@ -14,5 +14,5 @@ async def nested_handler(): core_router.include_router(nested_router) -app = FastAPI(lifespan=core_router.lifespan_context) +app = FastAPI() app.include_router(core_router) diff --git a/docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py index 379f78561c..465b436989 100644 --- a/docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py +++ b/docs/docs_src/integrations/fastapi/rabbit/multiple_lifespan.py @@ -1,19 +1,11 @@ -from contextlib import asynccontextmanager - from fastapi import FastAPI from faststream.rabbit.fastapi import RabbitRouter -core_router = RabbitRouter() -nested_router = RabbitRouter() +one_router = RabbitRouter() +another_router = RabbitRouter() -@asynccontextmanager -async def lifespan(app: FastAPI): - async with ( - core_router.lifespan_context(app), - nested_router.lifespan_context(app), - ): - yield +... -app = FastAPI(lifespan=lifespan) -app.include_router(core_router) -app.include_router(nested_router) +app = FastAPI() +app.include_router(one_router) +app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/rabbit/send.py b/docs/docs_src/integrations/fastapi/rabbit/send.py index f9ba45adfa..e47c73530d 100644 --- a/docs/docs_src/integrations/fastapi/rabbit/send.py +++ b/docs/docs_src/integrations/fastapi/rabbit/send.py @@ -4,7 +4,7 @@ router = RabbitRouter("amqp://guest:guest@localhost:5672/") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() @router.get("/") diff --git a/docs/docs_src/integrations/fastapi/rabbit/startup.py b/docs/docs_src/integrations/fastapi/rabbit/startup.py index dfa5361072..88c9c614f1 100644 --- a/docs/docs_src/integrations/fastapi/rabbit/startup.py +++ b/docs/docs_src/integrations/fastapi/rabbit/startup.py @@ -15,5 +15,5 @@ async def test(app: FastAPI): await router.broker.publish("Hello!", "test") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/redis/base.py b/docs/docs_src/integrations/fastapi/redis/base.py index 41c4f08b07..fb32b3e5d7 100644 --- a/docs/docs_src/integrations/fastapi/redis/base.py +++ b/docs/docs_src/integrations/fastapi/redis/base.py @@ -21,5 +21,5 @@ async def hello(m: Incoming, logger: Logger, d=Depends(call)): async def hello_http(): return "Hello, HTTP!" -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/docs_src/integrations/fastapi/redis/depends.py b/docs/docs_src/integrations/fastapi/redis/depends.py index 11c3f82c46..c896b68d74 100644 --- a/docs/docs_src/integrations/fastapi/redis/depends.py +++ b/docs/docs_src/integrations/fastapi/redis/depends.py @@ -5,7 +5,7 @@ router = fastapi.RedisRouter("redis://localhost:6379") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() def broker(): diff --git a/docs/docs_src/integrations/fastapi/redis/multiple.py b/docs/docs_src/integrations/fastapi/redis/multiple.py index 84c3d2431b..7092c93a3d 100644 --- a/docs/docs_src/integrations/fastapi/redis/multiple.py +++ b/docs/docs_src/integrations/fastapi/redis/multiple.py @@ -14,5 +14,5 @@ async def nested_handler(): core_router.include_router(nested_router) -app = FastAPI(lifespan=core_router.lifespan_context) +app = FastAPI() app.include_router(core_router) diff --git a/docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py b/docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py index 2180d10d6d..ad6ce39fbc 100644 --- a/docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py +++ b/docs/docs_src/integrations/fastapi/redis/multiple_lifespan.py @@ -1,19 +1,11 @@ -from contextlib import asynccontextmanager - from fastapi import FastAPI from faststream.redis.fastapi import RedisRouter -core_router = RedisRouter() -nested_router = RedisRouter() +one_router = RedisRouter() +another_router = RedisRouter() -@asynccontextmanager -async def lifespan(app: FastAPI): - async with ( - core_router.lifespan_context(app), - nested_router.lifespan_context(app), - ): - yield +... -app = FastAPI(lifespan=lifespan) -app.include_router(core_router) -app.include_router(nested_router) +app = FastAPI() +app.include_router(one_router) +app.include_router(another_router) diff --git a/docs/docs_src/integrations/fastapi/redis/send.py b/docs/docs_src/integrations/fastapi/redis/send.py index 9dffe82e6d..f5467ce9ea 100644 --- a/docs/docs_src/integrations/fastapi/redis/send.py +++ b/docs/docs_src/integrations/fastapi/redis/send.py @@ -4,7 +4,7 @@ router = RedisRouter("redis://localhost:6379") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() @router.get("/") diff --git a/docs/docs_src/integrations/fastapi/redis/startup.py b/docs/docs_src/integrations/fastapi/redis/startup.py index 00dc0906aa..0db1efdfaf 100644 --- a/docs/docs_src/integrations/fastapi/redis/startup.py +++ b/docs/docs_src/integrations/fastapi/redis/startup.py @@ -15,5 +15,5 @@ async def test(app: FastAPI): await router.broker.publish("Hello!", "test") -app = FastAPI(lifespan=router.lifespan_context) +app = FastAPI() app.include_router(router) diff --git a/docs/includes/getting_started/integrations/fastapi/multiple_lifespan.md b/docs/includes/getting_started/integrations/fastapi/multiple_lifespan.md index ce62563bfd..3867657ac7 100644 --- a/docs/includes/getting_started/integrations/fastapi/multiple_lifespan.md +++ b/docs/includes/getting_started/integrations/fastapi/multiple_lifespan.md @@ -1,29 +1,29 @@ === "AIOKafka" - ```python linenums="1" hl_lines="9-15 17" + ```python linenums="1" hl_lines="9-11" {!> docs_src/integrations/fastapi/kafka/multiple_lifespan.py !} ``` === "Confluent" - ```python linenums="1" hl_lines="9-15 17" + ```python linenums="1" hl_lines="9-11" {!> docs_src/integrations/fastapi/confluent/multiple_lifespan.py !} ``` === "RabbitMQ" - ```python linenums="1" hl_lines="9-15 17" + ```python linenums="1" hl_lines="9-11" {!> docs_src/integrations/fastapi/rabbit/multiple_lifespan.py !} ``` === "NATS" - ```python linenums="1" hl_lines="9-15 17" + ```python linenums="1" hl_lines="9-11" {!> docs_src/integrations/fastapi/nats/multiple_lifespan.py !} ``` === "Redis" - ```python linenums="1" hl_lines="9-15 17" + ```python linenums="1" hl_lines="9-11" {!> docs_src/integrations/fastapi/redis/multiple_lifespan.py !} ``` diff --git a/faststream/app.py b/faststream/app.py index 65449e247d..658b059ee0 100644 --- a/faststream/app.py +++ b/faststream/app.py @@ -249,6 +249,4 @@ async def catch_startup_validation_error() -> AsyncIterator[None]: raise ValidationError(fields=fields) from e except ImportError: - from faststream.utils.functions import fake_context - catch_startup_validation_error = fake_context diff --git a/faststream/broker/fastapi/router.py b/faststream/broker/fastapi/router.py index 420bfe962c..d5313c22ee 100644 --- a/faststream/broker/fastapi/router.py +++ b/faststream/broker/fastapi/router.py @@ -41,7 +41,7 @@ T_HandlerReturn, ) from faststream.utils.context.repository import context -from faststream.utils.functions import to_async +from faststream.utils.functions import fake_context, to_async if TYPE_CHECKING: from types import TracebackType @@ -63,7 +63,7 @@ class _BackgroundMiddleware(BaseMiddleware): - async def after_processed( + async def __aexit__( self, exc_type: Optional[Type[BaseException]] = None, exc_val: Optional[BaseException] = None, @@ -497,6 +497,7 @@ def include_router( ) -> None: """Includes a router in the API.""" if isinstance(router, StreamRouter): # pragma: no branch + router.lifespan_context = fake_context self.broker.include_router(router.broker) router.weak_dependencies_provider = self.weak_dependencies_provider diff --git a/faststream/confluent/schemas/partition.py b/faststream/confluent/schemas/partition.py index 1ec81e0355..cdedd143b2 100644 --- a/faststream/confluent/schemas/partition.py +++ b/faststream/confluent/schemas/partition.py @@ -1,6 +1,15 @@ from typing import Optional from confluent_kafka import TopicPartition as ConfluentPartition +from typing_extensions import NotRequired, TypedDict + + +class _TopicKwargs(TypedDict): + topic: str + partition: int + offset: int + metadata: NotRequired[str] + leader_epoch: NotRequired[int] class TopicPartition: @@ -27,7 +36,7 @@ def __init__( self.leader_epoch = leader_epoch def to_confluent(self) -> ConfluentPartition: - kwargs = { + kwargs: _TopicKwargs = { "topic": self.topic, "partition": self.partition, "offset": self.offset, diff --git a/pyproject.toml b/pyproject.toml index 8e822922b9..867804b23b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -127,7 +127,7 @@ test-core = [ testing = [ "faststream[test-core]", - "fastapi==0.112.1", + "fastapi==0.112.2", "pydantic-settings>=2.0.0,<3.0.0", "httpx==0.27.2", "PyYAML==6.0.2", diff --git a/tests/a_docs/integration/fastapi/test_multiple_lifespan.py b/tests/a_docs/integration/fastapi/test_multiple_lifespan.py index 4b909b411d..35cc66ddfa 100644 --- a/tests/a_docs/integration/fastapi/test_multiple_lifespan.py +++ b/tests/a_docs/integration/fastapi/test_multiple_lifespan.py @@ -40,12 +40,12 @@ class TestKafka(BaseCase): @pytest.fixture(scope="class") def data(self): from docs.docs_src.integrations.fastapi.kafka.multiple_lifespan import ( + another_router, app, - core_router, - nested_router, + one_router, ) - return (app, core_router, nested_router) + return (app, one_router, another_router) @pytest.mark.confluent @@ -54,12 +54,12 @@ class TestConfluent(BaseCase): @pytest.fixture(scope="class") def data(self): from docs.docs_src.integrations.fastapi.confluent.multiple_lifespan import ( + another_router, app, - core_router, - nested_router, + one_router, ) - return (app, core_router, nested_router) + return (app, one_router, another_router) @pytest.mark.nats @@ -68,12 +68,12 @@ class TestNats(BaseCase): @pytest.fixture(scope="class") def data(self): from docs.docs_src.integrations.fastapi.nats.multiple_lifespan import ( + another_router, app, - core_router, - nested_router, + one_router, ) - return (app, core_router, nested_router) + return (app, one_router, another_router) @pytest.mark.rabbit @@ -82,12 +82,12 @@ class TestRabbit(BaseCase): @pytest.fixture(scope="class") def data(self): from docs.docs_src.integrations.fastapi.rabbit.multiple_lifespan import ( + another_router, app, - core_router, - nested_router, + one_router, ) - return (app, core_router, nested_router) + return (app, one_router, another_router) @pytest.mark.redis @@ -96,9 +96,9 @@ class TestRedis(BaseCase): @pytest.fixture(scope="class") def data(self): from docs.docs_src.integrations.fastapi.redis.multiple_lifespan import ( + another_router, app, - core_router, - nested_router, + one_router, ) - return (app, core_router, nested_router) + return (app, one_router, another_router) diff --git a/tests/asyncapi/base/fastapi.py b/tests/asyncapi/base/fastapi.py index f374818941..58ded71515 100644 --- a/tests/asyncapi/base/fastapi.py +++ b/tests/asyncapi/base/fastapi.py @@ -26,7 +26,6 @@ async def test_fastapi_full_information(self): ) app = FastAPI( - lifespan=broker.lifespan_context, title="CustomApp", version="1.1.1", description="Test description", @@ -75,7 +74,7 @@ async def test_fastapi_asyncapi_routes(self): @broker.subscriber("test") async def handler(): ... - app = FastAPI(lifespan=broker.lifespan_context) + app = FastAPI() app.include_router(broker) async with self.broker_wrapper(broker.broker): @@ -95,7 +94,7 @@ async def handler(): ... async def test_fastapi_asyncapi_not_fount(self): broker = self.broker_class(include_in_schema=False) - app = FastAPI(lifespan=broker.lifespan_context) + app = FastAPI() app.include_router(broker) async with self.broker_wrapper(broker.broker): @@ -113,7 +112,7 @@ async def test_fastapi_asyncapi_not_fount(self): async def test_fastapi_asyncapi_not_fount_by_url(self): broker = self.broker_class(schema_url=None) - app = FastAPI(lifespan=broker.lifespan_context) + app = FastAPI() app.include_router(broker) async with self.broker_wrapper(broker.broker): diff --git a/tests/brokers/base/fastapi.py b/tests/brokers/base/fastapi.py index adb9e0c923..69ed8af4dc 100644 --- a/tests/brokers/base/fastapi.py +++ b/tests/brokers/base/fastapi.py @@ -203,7 +203,8 @@ class FastAPILocalTestcase(BaseTestcaseConfig): async def test_base(self, queue: str): router = self.router_class() - app = FastAPI(lifespan=router.lifespan_context) + app = FastAPI() + app.include_router(router) args, kwargs = self.get_subscriber_params(queue) @@ -226,7 +227,7 @@ async def hello(): async def test_base_without_state(self, queue: str): router = self.router_class(setup_state=False) - app = FastAPI(lifespan=router.lifespan_context) + app = FastAPI() args, kwargs = self.get_subscriber_params(queue) @@ -249,7 +250,7 @@ async def hello(): async def test_invalid(self, queue: str): router = self.router_class() - app = FastAPI(lifespan=router.lifespan_context) + app = FastAPI() args, kwargs = self.get_subscriber_params(queue) @@ -381,7 +382,7 @@ async def hello(a): async def test_hooks(self, mock: Mock): router = self.router_class() - app = FastAPI(lifespan=router.lifespan_context) + app = FastAPI() app.include_router(router) @router.after_startup @@ -417,7 +418,7 @@ async def lifespan(app): router = self.router_class(lifespan=lifespan) - app = FastAPI(lifespan=router.lifespan_context) + app = FastAPI() app.include_router(router) async with self.broker_test(router.broker), router.lifespan_context( @@ -462,7 +463,7 @@ async def test_include(self, queue: str): router = self.router_class() router2 = self.router_class() - app = FastAPI(lifespan=router.lifespan_context) + app = FastAPI() args, kwargs = self.get_subscriber_params(queue) @@ -477,6 +478,7 @@ async def hello_router2(): return "hi" router.include_router(router2) + app.include_router(router) async with self.broker_test(router.broker): with TestClient(app) as client: @@ -506,7 +508,7 @@ def dep1(): mock.not_call() pass - app = FastAPI(lifespan=router.lifespan_context) + app = FastAPI() app.dependency_overrides[dep1] = lambda: mock() args, kwargs = self.get_subscriber_params(queue)