Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed method hiding. #20

Merged
merged 1 commit into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]["/"]
Loading