Skip to content

Commit

Permalink
Fixed method hiding.
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kirilin <[email protected]>
  • Loading branch information
s3rius committed Feb 5, 2024
1 parent e84f4ba commit 8d62c63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
4 changes: 2 additions & 2 deletions aiohttp_deps/swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]["/"]

0 comments on commit 8d62c63

Please sign in to comment.