Skip to content

Commit 83d51d0

Browse files
authored
Merge pull request #33 from olexandr-klymenko/master
Add support for aiohttp Nested Applications
2 parents dadc55c + 0bea0dd commit 83d51d0

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed

aiohttp_swagger/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,10 @@ def setup_swagger(app: web.Application,
9191
with open(join(STATIC_PATH, "index.html"), "r") as f:
9292
app["SWAGGER_TEMPLATE_CONTENT"] = (
9393
f.read()
94-
.replace("##SWAGGER_CONFIG##", _swagger_def_url)
95-
.replace("##STATIC_PATH##", statics_path)
94+
.replace("##SWAGGER_CONFIG##", '/{}{}'.
95+
format(api_base_url.lstrip('/'), _swagger_def_url))
96+
.replace("##STATIC_PATH##", '/{}{}'.
97+
format(api_base_url.lstrip('/'), statics_path))
9698
)
9799

98100

doc/source/customizing.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,33 @@ Global Swagger YAML
166166
167167
setup_swagger(app, swagger_from_file="example_swagger.yaml") # <-- Loaded Swagger from external YAML file
168168
169+
web.run_app(app, host="127.0.0.1")
170+
171+
Nested applications
172+
+++++++++++++++++++
173+
174+
:samp:`aiohttp-swagger` is compatible with aiohttp `Nested applications <http://aiohttp.readthedocs.io/en/stable/web.html>`_ feature.
175+
In this case `api_base_url` argument of `setup_swagger` function should be the same as `prefix` argument of `add_subapp` method:
176+
177+
178+
.. code-block:: python
179+
180+
from aiohttp import web
181+
from aiohttp_swagger import *
182+
183+
async def ping(request):
184+
return web.Response(text="pong")
185+
186+
sub_app = web.Application()
187+
188+
sub_app.router.add_route('GET', "/ping", ping)
189+
190+
setup_swagger(sub_app,
191+
swagger_from_file="example_swagger.yaml",
192+
api_base_url='/sub_app_prefix')
193+
194+
app = web.Application()
195+
196+
app.add_subapp(prefix='/sub_app_prefix', subapp=sub_app)
197+
169198
web.run_app(app, host="127.0.0.1")

tests/test_swagger.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,25 @@ def test_class_view(test_client, loop):
272272
assert "/class_view" in result['paths']
273273
assert "patch" not in result['paths']["/class_view"]
274274

275+
276+
@asyncio.coroutine
277+
def test_sub_app(test_client, loop):
278+
sub_app = web.Application(loop=loop)
279+
sub_app.router.add_route('*', "/class_view", ClassView)
280+
setup_swagger(sub_app, api_base_url='/sub_app')
281+
app = web.Application(loop=loop)
282+
app.add_subapp(prefix='/sub_app', subapp=sub_app)
283+
284+
client = yield from test_client(app)
285+
# GET
286+
resp = yield from client.get('/sub_app/class_view')
287+
assert resp.status == 200
288+
text = yield from resp.text()
289+
assert 'OK' in text
290+
swagger_resp1 = yield from client.get('/sub_app/api/doc/swagger.json')
291+
assert swagger_resp1.status == 200
292+
text = yield from swagger_resp1.text()
293+
result = json.loads(text)
294+
assert "/class_view" in result['paths']
295+
assert "get" in result['paths']["/class_view"]
296+
assert "post" in result['paths']["/class_view"]

0 commit comments

Comments
 (0)