Skip to content

Commit

Permalink
Add json_only keyword to allow serving only swagger.json and not stat…
Browse files Browse the repository at this point in the history
…ic files

Add a test
  • Loading branch information
ukch committed Apr 13, 2018
1 parent 8af93ac commit eeb6790
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
12 changes: 9 additions & 3 deletions aiohttp_swagger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def setup_swagger(app: web.Application,
contact: str = "",
swagger_home_decor: FunctionType = None,
swagger_def_decor: FunctionType = None,
swagger_info: dict = None):
swagger_info: dict = None,
json_only: bool = False):
_swagger_url = ("/{}".format(swagger_url)
if not swagger_url.startswith("/")
else swagger_url)
Expand Down Expand Up @@ -75,10 +76,16 @@ def setup_swagger(app: web.Application,
_swagger_def_func = swagger_def_decor(_swagger_def)

# Add API routes
app.router.add_route('GET', _swagger_def_url, _swagger_def_func)

app["SWAGGER_DEF_CONTENT"] = swagger_info

if json_only:
return

app.router.add_route('GET', _swagger_url, _swagger_home_func)
app.router.add_route('GET', "{}/".format(_base_swagger_url),
_swagger_home_func)
app.router.add_route('GET', _swagger_def_url, _swagger_def_func)

# Set statics
statics_path = '{}/swagger_static'.format(_base_swagger_url)
Expand All @@ -87,7 +94,6 @@ def setup_swagger(app: web.Application,
# --------------------------------------------------------------------------
# Build templates
# --------------------------------------------------------------------------
app["SWAGGER_DEF_CONTENT"] = swagger_info
with open(join(STATIC_PATH, "index.html"), "r") as f:
app["SWAGGER_TEMPLATE_CONTENT"] = (
f.read()
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# aiohttp-swagger
Expand Down
43 changes: 34 additions & 9 deletions tests/test_swagger.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import asyncio
import json
import pathlib
import pytest
import yaml
from os.path import join, dirname, abspath
Expand Down Expand Up @@ -101,7 +102,8 @@ def test_swagger_file_url(test_client, loop):

app = web.Application(loop=loop)
setup_swagger(app,
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml")
swagger_from_file=TESTS_PATH + "/data/example_swagger.yaml",
json_only=True)

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

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

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

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

client = yield from test_client(app)
resp1 = yield from client.get('/api/v1/doc/swagger.json')
Expand All @@ -205,7 +210,8 @@ def test_swagger_info(test_client, loop, swagger_info):
description = "Test Custom Swagger"
setup_swagger(app,
swagger_url="/api/v1/doc",
swagger_info=swagger_info)
swagger_info=swagger_info,
json_only=True)

client = yield from test_client(app)
resp1 = yield from client.get('/api/v1/doc/swagger.json')
Expand All @@ -221,7 +227,7 @@ def test_swagger_info(test_client, loop, swagger_info):
def test_undocumented_fn(test_client, loop):
app = web.Application(loop=loop)
app.router.add_route('GET', "/undoc_ping", undoc_ping)
setup_swagger(app)
setup_swagger(app, json_only=True)
client = yield from test_client(app)
resp = yield from client.get('/undoc_ping')
assert resp.status == 200
Expand All @@ -235,7 +241,7 @@ def test_undocumented_fn(test_client, loop):
def test_class_view(test_client, loop):
app = web.Application(loop=loop)
app.router.add_route('*', "/class_view", ClassView)
setup_swagger(app)
setup_swagger(app, json_only=True)

client = yield from test_client(app)
# GET
Expand Down Expand Up @@ -277,7 +283,7 @@ def test_class_view(test_client, loop):
def test_sub_app(test_client, loop):
sub_app = web.Application(loop=loop)
sub_app.router.add_route('*', "/class_view", ClassView)
setup_swagger(sub_app, api_base_url='/sub_app')
setup_swagger(sub_app, api_base_url='/sub_app', json_only=True)
app = web.Application(loop=loop)
app.add_subapp(prefix='/sub_app', subapp=sub_app)

Expand All @@ -294,3 +300,22 @@ def test_sub_app(test_client, loop):
assert "/class_view" in result['paths']
assert "get" in result['paths']["/class_view"]
assert "post" in result['paths']["/class_view"]


@asyncio.coroutine
def test_swagger_defined_paths(test_client, loop):
app1 = web.Application(loop=loop)
setup_swagger(app1, json_only=True)
urls1 = [r.get_info() for r in app1.router.resources()]
assert urls1 == [{'path': '/api/doc/swagger.json'}]

app2 = web.Application(loop=loop)
setup_swagger(app2)
urls2 = [r.get_info() for r in app2.router.resources()]
assert urls2 == [
{'path': '/api/doc/swagger.json'},
{'path': '/api/doc'},
{'path': '/api/doc/'},
{'directory': pathlib.Path('aiohttp_swagger/swagger_ui').absolute(),
'prefix': '/api/doc/swagger_static'},
]

0 comments on commit eeb6790

Please sign in to comment.