Skip to content

Commit

Permalink
refactor: remove global broker from context
Browse files Browse the repository at this point in the history
  • Loading branch information
Lancetnik committed Sep 16, 2024
1 parent 2a978c9 commit 5d4cac1
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
10 changes: 4 additions & 6 deletions faststream/_internal/broker/broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
from typing_extensions import Annotated, Doc, Self

from faststream._internal._compat import is_test_env
from faststream._internal.context.repository import context
from faststream._internal.log.logging import set_logger_fmt
from faststream._internal.proto import SetupAble
from faststream._internal.subscriber.proto import SubscriberProto
Expand Down Expand Up @@ -185,10 +184,6 @@ def __init__(
*self._middlewares,
)

# TODO: move this context to Handlers' extra_context to support multiple brokers
context.set_global("logger", self.logger)
context.set_global("broker", self)

# FastDepends args
self._is_apply_types = apply_types
self._is_validate = validate
Expand Down Expand Up @@ -269,7 +264,10 @@ def _subscriber_setup_extra(self) -> "AnyDict":
"logger": self.logger,
"producer": self._producer,
"graceful_timeout": self.graceful_timeout,
"extra_context": {},
"extra_context": {
"broker": self,
"logger": self.logger,
},
# broker options
"broker_parser": self._parser,
"broker_decoder": self._decoder,
Expand Down
67 changes: 33 additions & 34 deletions tests/asyncapi/base/v2_6_0/arguments.py
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,39 @@ async def handle(user: Model): ...
},
}, schema["components"]

def test_with_filter(self):
class User(pydantic.BaseModel):
name: str = ""
id: int

broker = self.broker_class()

sub = broker.subscriber("test")

@sub(
filter=lambda m: m.content_type == "application/json",
)
async def handle(id: int): ...

@sub
async def handle_default(msg): ...

schema = get_app_schema(self.build_app(broker), version="2.6.0").to_jsonable()

assert (
len(
next(iter(schema["components"]["messages"].values()))["payload"][
"oneOf"
]
)
== 2
)

payload = schema["components"]["schemas"]

assert "Handle:Message:Payload" in list(payload.keys())
assert "HandleDefault:Message:Payload" in list(payload.keys())


class ArgumentsTestcase(FastAPICompatible):
dependency_builder = staticmethod(Depends)
Expand Down Expand Up @@ -635,37 +668,3 @@ async def handle(id: int, user: Optional[str] = None, message=Context()): ...
"type": "object",
}
)

def test_with_filter(self):
# TODO: move it to FastAPICompatible with FastAPI refactore
class User(pydantic.BaseModel):
name: str = ""
id: int

broker = self.broker_class()

sub = broker.subscriber("test")

@sub(
filter=lambda m: m.content_type == "application/json",
)
async def handle(id: int): ...

@sub
async def handle_default(msg): ...

schema = get_app_schema(self.build_app(broker), version="2.6.0").to_jsonable()

assert (
len(
next(iter(schema["components"]["messages"].values()))["payload"][
"oneOf"
]
)
== 2
)

payload = schema["components"]["schemas"]

assert "Handle:Message:Payload" in list(payload.keys())
assert "HandleDefault:Message:Payload" in list(payload.keys())

0 comments on commit 5d4cac1

Please sign in to comment.