From 0ebdc86a7bb8f41d1276adfe9076a0ada60b4fe3 Mon Sep 17 00:00:00 2001 From: Artur Gwiazda Date: Mon, 24 Aug 2020 14:37:23 +0200 Subject: [PATCH 1/2] Relative url param. --- aiohttp_apispec/aiohttp_apispec.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/aiohttp_apispec/aiohttp_apispec.py b/aiohttp_apispec/aiohttp_apispec.py index c6d38bf..6401c07 100644 --- a/aiohttp_apispec/aiohttp_apispec.py +++ b/aiohttp_apispec/aiohttp_apispec.py @@ -93,7 +93,10 @@ async def doc_routes(app_): async def swagger_handler(request): return web.json_response(request.app["swagger_dict"]) - app.router.add_route("GET", self.url, swagger_handler, name=NAME_SWAGGER_SPEC) + route_url = self.url + if not self.url.startswith("/"): + route_url = "/{}".format(route_url) + app.router.add_route("GET", route_url, swagger_handler, name=NAME_SWAGGER_SPEC) if self.swagger_path is not None: self._add_swagger_web_page(app, self.static_path, self.swagger_path) From 3fe6a73e055debc2170f080f8abce8d7678773a3 Mon Sep 17 00:00:00 2001 From: Artur Gwiazda Date: Tue, 25 Aug 2020 07:38:51 +0200 Subject: [PATCH 2/2] Test added. --- aiohttp_apispec/aiohttp_apispec.py | 2 +- tests/test_documentation.py | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/aiohttp_apispec/aiohttp_apispec.py b/aiohttp_apispec/aiohttp_apispec.py index 6401c07..9950a12 100644 --- a/aiohttp_apispec/aiohttp_apispec.py +++ b/aiohttp_apispec/aiohttp_apispec.py @@ -95,7 +95,7 @@ async def swagger_handler(request): route_url = self.url if not self.url.startswith("/"): - route_url = "/{}".format(route_url) + route_url = "/{}".format(self.url) app.router.add_route("GET", route_url, swagger_handler, name=NAME_SWAGGER_SPEC) if self.swagger_path is not None: diff --git a/tests/test_documentation.py b/tests/test_documentation.py index 9c3f841..da60fe1 100644 --- a/tests/test_documentation.py +++ b/tests/test_documentation.py @@ -164,3 +164,11 @@ async def test_not_register_route_for_none_url(): setup_aiohttp_apispec(app=app, url=None) routes_count_after_setup_apispec = len(app.router.routes()) assert routes_count == routes_count_after_setup_apispec + + +async def test_register_route_for_relative_url(): + app = web.Application() + routes_count = len(app.router.routes()) + setup_aiohttp_apispec(app=app, url="api/swagger") + routes_count_after_setup_apispec = len(app.router.routes()) + assert routes_count == routes_count_after_setup_apispec