From 8d62c630e2a2c14b52f71b36d0366d5e21598202 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Mon, 5 Feb 2024 21:21:56 +0100 Subject: [PATCH] Fixed method hiding. Signed-off-by: Pavel Kirilin --- aiohttp_deps/swagger.py | 4 ++-- tests/test_swagger.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/aiohttp_deps/swagger.py b/aiohttp_deps/swagger.py index 8552b9d..23f3eed 100644 --- a/aiohttp_deps/swagger.py +++ b/aiohttp_deps/swagger.py @@ -247,9 +247,9 @@ async def event_handler(app: web.Application) -> None: # noqa: C901 for route in app.router.routes(): if route.resource is None: # pragma: no cover continue - if hide_heads and route.method.lower() == "HEAD": # pragma: no cover + if hide_heads and route.method.upper() == "HEAD": continue - if hide_options and route.method.lower() == "OPTIONS": # pragma: no cover + if hide_options and route.method.upper() == "OPTIONS": continue if isinstance(route._handler, InjectableFuncHandler): extra_openapi = getattr( diff --git a/tests/test_swagger.py b/tests/test_swagger.py index 4e95549..550cef8 100644 --- a/tests/test_swagger.py +++ b/tests/test_swagger.py @@ -750,3 +750,33 @@ async def my_handler(param: TestModel = Depends(Json())) -> None: "schema" ]["properties"]["mt"]["type"] assert oapi_validation_type == validation_type + + +@pytest.mark.anyio +@pytest.mark.parametrize( + ["method", "option_name"], + [("HEAD", "hide_heads"), ("OPTIONS", "hide_options")], +) +async def test_method_skips( + my_app: web.Application, + aiohttp_client: ClientGenerator, + method: str, + option_name: str, +) -> None: + openapi_url = "/my_api_def.json" + my_app.on_startup.append( + setup_swagger(schema_url=openapi_url, **{option_name: True}), + ) + + async def my_handler() -> None: + """Nothing.""" + return web.Response() + + my_app.router.add_route(method, "/", my_handler) + my_app.router.add_route("GET", "/", my_handler) + + client = await aiohttp_client(my_app) + response = await client.get(openapi_url) + schema = await response.json() + assert "get" in schema["paths"]["/"] + assert method.lower() not in schema["paths"]["/"]