Skip to content

Commit eeb6790

Browse files
committed
Add json_only keyword to allow serving only swagger.json and not static files
Add a test
1 parent 8af93ac commit eeb6790

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

aiohttp_swagger/__init__.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ def setup_swagger(app: web.Application,
4646
contact: str = "",
4747
swagger_home_decor: FunctionType = None,
4848
swagger_def_decor: FunctionType = None,
49-
swagger_info: dict = None):
49+
swagger_info: dict = None,
50+
json_only: bool = False):
5051
_swagger_url = ("/{}".format(swagger_url)
5152
if not swagger_url.startswith("/")
5253
else swagger_url)
@@ -75,10 +76,16 @@ def setup_swagger(app: web.Application,
7576
_swagger_def_func = swagger_def_decor(_swagger_def)
7677

7778
# Add API routes
79+
app.router.add_route('GET', _swagger_def_url, _swagger_def_func)
80+
81+
app["SWAGGER_DEF_CONTENT"] = swagger_info
82+
83+
if json_only:
84+
return
85+
7886
app.router.add_route('GET', _swagger_url, _swagger_home_func)
7987
app.router.add_route('GET', "{}/".format(_base_swagger_url),
8088
_swagger_home_func)
81-
app.router.add_route('GET', _swagger_def_url, _swagger_def_func)
8289

8390
# Set statics
8491
statics_path = '{}/swagger_static'.format(_base_swagger_url)
@@ -87,7 +94,6 @@ def setup_swagger(app: web.Application,
8794
# --------------------------------------------------------------------------
8895
# Build templates
8996
# --------------------------------------------------------------------------
90-
app["SWAGGER_DEF_CONTENT"] = swagger_info
9197
with open(join(STATIC_PATH, "index.html"), "r") as f:
9298
app["SWAGGER_TEMPLATE_CONTENT"] = (
9399
f.read()

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env python
12
# -*- coding: utf-8 -*-
23
#
34
# aiohttp-swagger

tests/test_swagger.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import asyncio
22
import json
3+
import pathlib
34
import pytest
45
import yaml
56
from os.path import join, dirname, abspath
@@ -101,7 +102,8 @@ def test_swagger_file_url(test_client, loop):
101102

102103
app = web.Application(loop=loop)
103104
setup_swagger(app,
104-
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml")
105+
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml",
106+
json_only=True)
105107

106108
client = yield from test_client(app)
107109
resp1 = yield from client.get('/api/doc/swagger.json')
@@ -117,7 +119,7 @@ def test_swagger_file_url(test_client, loop):
117119
def test_partial_swagger_file(test_client, loop):
118120
app = web.Application(loop=loop)
119121
app.router.add_route('GET', "/ping-partial", ping_partial)
120-
setup_swagger(app)
122+
setup_swagger(app, json_only=True)
121123

122124
client = yield from test_client(app)
123125
resp1 = yield from client.get('/api/doc/swagger.json')
@@ -137,7 +139,8 @@ def test_custom_swagger(test_client, loop):
137139
description=description,
138140
title="Test Custom Title",
139141
api_version="1.0.0",
140-
contact="[email protected]")
142+
contact="[email protected]",
143+
json_only=True)
141144

142145
client = yield from test_client(app)
143146
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -159,7 +162,8 @@ def test_swagger_home_decorator(test_client, loop):
159162
title="Test Custom Title",
160163
api_version="1.0.0",
161164
contact="[email protected]",
162-
swagger_home_decor=lambda x: x)
165+
swagger_home_decor=lambda x: x,
166+
json_only=True)
163167

164168
client = yield from test_client(app)
165169
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -181,7 +185,8 @@ def test_swagger_def_decorator(test_client, loop):
181185
title="Test Custom Title",
182186
api_version="1.0.0",
183187
contact="[email protected]",
184-
swagger_def_decor=lambda x: x)
188+
swagger_def_decor=lambda x: x,
189+
json_only=True)
185190

186191
client = yield from test_client(app)
187192
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -205,7 +210,8 @@ def test_swagger_info(test_client, loop, swagger_info):
205210
description = "Test Custom Swagger"
206211
setup_swagger(app,
207212
swagger_url="/api/v1/doc",
208-
swagger_info=swagger_info)
213+
swagger_info=swagger_info,
214+
json_only=True)
209215

210216
client = yield from test_client(app)
211217
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -221,7 +227,7 @@ def test_swagger_info(test_client, loop, swagger_info):
221227
def test_undocumented_fn(test_client, loop):
222228
app = web.Application(loop=loop)
223229
app.router.add_route('GET', "/undoc_ping", undoc_ping)
224-
setup_swagger(app)
230+
setup_swagger(app, json_only=True)
225231
client = yield from test_client(app)
226232
resp = yield from client.get('/undoc_ping')
227233
assert resp.status == 200
@@ -235,7 +241,7 @@ def test_undocumented_fn(test_client, loop):
235241
def test_class_view(test_client, loop):
236242
app = web.Application(loop=loop)
237243
app.router.add_route('*', "/class_view", ClassView)
238-
setup_swagger(app)
244+
setup_swagger(app, json_only=True)
239245

240246
client = yield from test_client(app)
241247
# GET
@@ -277,7 +283,7 @@ def test_class_view(test_client, loop):
277283
def test_sub_app(test_client, loop):
278284
sub_app = web.Application(loop=loop)
279285
sub_app.router.add_route('*', "/class_view", ClassView)
280-
setup_swagger(sub_app, api_base_url='/sub_app')
286+
setup_swagger(sub_app, api_base_url='/sub_app', json_only=True)
281287
app = web.Application(loop=loop)
282288
app.add_subapp(prefix='/sub_app', subapp=sub_app)
283289

@@ -294,3 +300,22 @@ def test_sub_app(test_client, loop):
294300
assert "/class_view" in result['paths']
295301
assert "get" in result['paths']["/class_view"]
296302
assert "post" in result['paths']["/class_view"]
303+
304+
305+
@asyncio.coroutine
306+
def test_swagger_defined_paths(test_client, loop):
307+
app1 = web.Application(loop=loop)
308+
setup_swagger(app1, json_only=True)
309+
urls1 = [r.get_info() for r in app1.router.resources()]
310+
assert urls1 == [{'path': '/api/doc/swagger.json'}]
311+
312+
app2 = web.Application(loop=loop)
313+
setup_swagger(app2)
314+
urls2 = [r.get_info() for r in app2.router.resources()]
315+
assert urls2 == [
316+
{'path': '/api/doc/swagger.json'},
317+
{'path': '/api/doc'},
318+
{'path': '/api/doc/'},
319+
{'directory': pathlib.Path('aiohttp_swagger/swagger_ui').absolute(),
320+
'prefix': '/api/doc/swagger_static'},
321+
]

0 commit comments

Comments
 (0)