diff --git a/aiohttp_deps/initializer.py b/aiohttp_deps/initializer.py index 266968f..6b03cbd 100644 --- a/aiohttp_deps/initializer.py +++ b/aiohttp_deps/initializer.py @@ -30,6 +30,15 @@ def __init__( ) -> None: self.original_handler = copy.copy(original_route) self.graph = DependencyGraph(self.original_handler) + signature = inspect.signature(self.original_handler) + # This flag means that the function requires one argument and + # doesn't depend on any other dependencies. + # We assume that such functions should be treated as ordinary + # aiohttp handlers and therefore we don't inject any dependencies + # and pass request object directly to the handler. + self.is_ordinary = False + if self.graph.is_empty() and len(signature.parameters) == 1: + self.is_ordinary = True async def __call__(self, request: web.Request) -> web.StreamResponse: """ @@ -41,6 +50,8 @@ async def __call__(self, request: web.Request) -> web.StreamResponse: :param request: current request. :return: response. """ + if self.is_ordinary: + return await self.original_handler(request) # Hack for mypy to work values_overrides = request.app.get(VALUES_OVERRIDES_KEY) if values_overrides is None: diff --git a/aiohttp_deps/swagger.py b/aiohttp_deps/swagger.py index 11923d7..8552b9d 100644 --- a/aiohttp_deps/swagger.py +++ b/aiohttp_deps/swagger.py @@ -203,6 +203,7 @@ def setup_swagger( # noqa: C901 swagger_ui_url: str = "/docs", enable_ui: bool = True, hide_heads: bool = True, + hide_options: bool = True, title: str = "AioHTTP", description: Optional[str] = None, version: str = "1.0.0", @@ -225,13 +226,14 @@ def setup_swagger( # noqa: C901 :param swagger_ui_url: URL where swagger ui will be served. :param enable_ui: whether you want to enable bundled swagger ui. :param hide_heads: hide HEAD requests. + :param hide_options: hide OPTIONS requests. :param title: Title of an application. :param description: description of an application. :param version: version of an application. :return: startup event handler. """ - async def event_handler(app: web.Application) -> None: + async def event_handler(app: web.Application) -> None: # noqa: C901 openapi_schema = { "openapi": "3.0.0", "info": { @@ -245,7 +247,9 @@ async def event_handler(app: web.Application) -> None: for route in app.router.routes(): if route.resource is None: # pragma: no cover continue - if hide_heads and route.method == "HEAD": + if hide_heads and route.method.lower() == "HEAD": # pragma: no cover + continue + if hide_options and route.method.lower() == "OPTIONS": # pragma: no cover continue if isinstance(route._handler, InjectableFuncHandler): extra_openapi = getattr( diff --git a/pyproject.toml b/pyproject.toml index 7b052b5..b21f6f6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -3,7 +3,7 @@ name = "aiohttp-deps" description = "Dependency injection for AioHTTP" authors = ["Taskiq team "] maintainers = ["Taskiq team "] -version = "1.1.0" +version = "1.1.1" readme = "README.md" license = "LICENSE" classifiers = [ @@ -47,12 +47,9 @@ ruff = "^0.1.7" [tool.mypy] strict = true ignore_missing_imports = true -allow_subclassing_any = true -allow_untyped_calls = true pretty = true show_error_codes = true implicit_reexport = true -allow_untyped_decorators = true warn_return_any = false warn_unused_ignores = false diff --git a/tests/test_func_dependencies.py b/tests/test_func_dependencies.py index 7c50d2d..de333d3 100644 --- a/tests/test_func_dependencies.py +++ b/tests/test_func_dependencies.py @@ -79,3 +79,18 @@ async def handler(num: int = Depends(original_dep)) -> web.Response: resp = await client.get("/") assert resp.status == 200 assert (await resp.json())["request"] == 2 + + +@pytest.mark.anyio +async def test_ordinary_functions_support( + my_app: web.Application, + aiohttp_client: ClientGenerator, +) -> None: + async def handler(request: web.Request) -> web.Response: + return web.json_response({"request": "ordinary"}) + + my_app.router.add_get("/", handler) + client = await aiohttp_client(my_app) + resp = await client.get("/") + assert resp.status == 200 + assert await resp.json() == {"request": "ordinary"}