Skip to content

Commit 2a495af

Browse files
committed
Add json_only keyword to allow serving only swagger.json and not static files
Add a test
1 parent 5a59e86 commit 2a495af

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
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)
@@ -74,9 +75,15 @@ def setup_swagger(app: web.Application,
7475
_swagger_def_func = swagger_def_decor(_swagger_def)
7576

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

8188
# Set statics
8289
statics_path = '{}/swagger_static'.format(_swagger_url)
@@ -85,7 +92,6 @@ def setup_swagger(app: web.Application,
8592
# --------------------------------------------------------------------------
8693
# Build templates
8794
# --------------------------------------------------------------------------
88-
app["SWAGGER_DEF_CONTENT"] = swagger_info
8995
with open(join(STATIC_PATH, "index.html"), "r") as f:
9096
app["SWAGGER_TEMPLATE_CONTENT"] = (
9197
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: 31 additions & 6 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
@@ -50,7 +51,8 @@ def test_swagger_file_url(test_client, loop):
5051

5152
app = web.Application(loop=loop)
5253
setup_swagger(app,
53-
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml")
54+
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml",
55+
json_only=True)
5456

5557
client = yield from test_client(app)
5658
resp1 = yield from client.get('/api/doc/swagger.json')
@@ -66,7 +68,7 @@ def test_swagger_file_url(test_client, loop):
6668
def test_partial_swagger_file(test_client, loop):
6769
app = web.Application(loop=loop)
6870
app.router.add_route('GET', "/ping-partial", ping_partial)
69-
setup_swagger(app)
71+
setup_swagger(app, json_only=True)
7072

7173
client = yield from test_client(app)
7274
resp1 = yield from client.get('/api/doc/swagger.json')
@@ -86,7 +88,8 @@ def test_custom_swagger(test_client, loop):
8688
description=description,
8789
title="Test Custom Title",
8890
api_version="1.0.0",
89-
contact="my.custom.contact@example.com")
91+
contact="my.custom.contact@example.com",
92+
json_only=True)
9093

9194
client = yield from test_client(app)
9295
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -108,7 +111,8 @@ def test_swagger_home_decorator(test_client, loop):
108111
title="Test Custom Title",
109112
api_version="1.0.0",
110113
contact="my.custom.contact@example.com",
111-
swagger_home_decor=lambda x: x)
114+
swagger_home_decor=lambda x: x,
115+
json_only=True)
112116

113117
client = yield from test_client(app)
114118
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -130,7 +134,8 @@ def test_swagger_def_decorator(test_client, loop):
130134
title="Test Custom Title",
131135
api_version="1.0.0",
132136
contact="my.custom.contact@example.com",
133-
swagger_def_decor=lambda x: x)
137+
swagger_def_decor=lambda x: x,
138+
json_only=True)
134139

135140
client = yield from test_client(app)
136141
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -154,7 +159,8 @@ def test_swagger_info(test_client, loop, swagger_info):
154159
description = "Test Custom Swagger"
155160
setup_swagger(app,
156161
swagger_url="/api/v1/doc",
157-
swagger_info=swagger_info)
162+
swagger_info=swagger_info,
163+
json_only=True)
158164

159165
client = yield from test_client(app)
160166
resp1 = yield from client.get('/api/v1/doc/swagger.json')
@@ -164,3 +170,22 @@ def test_swagger_info(test_client, loop, swagger_info):
164170
assert '/example1' in result['paths']
165171
assert '/example2' in result['paths']
166172
assert 'API Title' in result['info']['title']
173+
174+
175+
@asyncio.coroutine
176+
def test_swagger_defined_paths(test_client, loop):
177+
app1 = web.Application(loop=loop)
178+
setup_swagger(app1, json_only=True)
179+
urls1 = [r.get_info() for r in app1.router.resources()]
180+
assert urls1 == [{'path': '/api/doc/swagger.json'}]
181+
182+
app2 = web.Application(loop=loop)
183+
setup_swagger(app2)
184+
urls2 = [r.get_info() for r in app2.router.resources()]
185+
assert urls2 == [
186+
{'path': '/api/doc/swagger.json'},
187+
{'path': '/api/doc'},
188+
{'path': '/api/doc/'},
189+
{'directory': pathlib.Path('aiohttp_swagger/swagger_ui').absolute(),
190+
'prefix': '/api/doc/swagger_static'},
191+
]

0 commit comments

Comments
 (0)