Skip to content

Commit 0164bf8

Browse files
committed
Enable Guards to be resolved as provider
1 parent 5668f65 commit 0164bf8

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

ellar/di/injector/ellar_injector.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414

1515

1616
class EllarInjector(Injector):
17-
__slots__ = ("_stack", "parent", "container", "_modules")
17+
__slots__ = ("_stack", "parent", "container", "_modules", "_auto_bind")
1818

1919
def __init__(
2020
self,
2121
auto_bind: bool = True,
2222
parent: "Injector" = None,
2323
) -> None:
2424
self._stack = ()
25-
25+
self._auto_bind = auto_bind
2626
self.parent = parent
2727
# Binder
2828
self.container = Container(
2929
self,
30-
auto_bind=auto_bind,
30+
auto_bind=self._auto_bind,
3131
parent=parent.binder if parent is not None else None,
3232
)
3333

@@ -78,7 +78,9 @@ def add_module(self, module_ref: "ModuleRefBase") -> None:
7878
async def create_request_service_provider(
7979
self,
8080
) -> t.AsyncGenerator[RequestServiceProvider, None]:
81-
request_provider = RequestServiceProvider(self.container, auto_bind=True)
81+
request_provider = RequestServiceProvider(
82+
self.container, auto_bind=self._auto_bind
83+
)
8284
try:
8385
yield request_provider
8486
finally:

tests/test_guard.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ def auth_demo_endpoint(request=Req()):
100100

101101
app.router.append(auth_demo_endpoint)
102102

103+
app.injector.container.register(HeaderSecretKeyCustomException)
104+
app.injector.container.register(QuerySecretKeyInjectable)
105+
app.injector.container.register(BearerAuth)
106+
app.injector.container.register(DigestAuth)
107+
103108
client = TestClient(app)
104109

105110
BODY_UNAUTHORIZED_DEFAULT = {"detail": "Not authenticated"}

tests/test_openapi/test_module.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def test_openapi_module_creates_redocs_endpoint():
9494

9595

9696
def test_openapi_module_with_route_guards():
97-
app = AppFactory.create_app()
97+
app = AppFactory.create_app(providers=[CustomDocsGuard])
9898
document = OpenAPIDocumentBuilder().build_document(app)
9999

100100
module_instance = app.install_module(

0 commit comments

Comments
 (0)