Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix swagger console backslash redirect for aiohttp (thanks @pando85) #843

Merged
merged 4 commits into from
Mar 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions connexion/apis/aiohttp_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
import aiohttp_jinja2
import jinja2
from aiohttp import web
from aiohttp.web_exceptions import HTTPNotFound
from aiohttp.web_exceptions import HTTPNotFound, HTTPPermanentRedirect
from aiohttp.web_middlewares import normalize_path_middleware
from connexion.apis.abstract import AbstractAPI
from connexion.exceptions import OAuthProblem, OAuthScopeProblem
from connexion.handlers import AuthErrorHandler
Expand Down Expand Up @@ -40,8 +41,19 @@ def oauth_problem_middleware(request, handler):

class AioHttpApi(AbstractAPI):
def __init__(self, *args, **kwargs):
# NOTE we use HTTPPermanentRedirect (308) because
# clients sometimes turn POST requests into GET requests
# on 301, 302, or 303
# see https://tools.ietf.org/html/rfc7538
trailing_slash_redirect = normalize_path_middleware(
append_slash=True,
redirect_class=HTTPPermanentRedirect
)
self.subapp = web.Application(
middlewares=[oauth_problem_middleware]
middlewares=[
oauth_problem_middleware,
trailing_slash_redirect
]
)
AbstractAPI.__init__(self, *args, **kwargs)

Expand Down Expand Up @@ -118,7 +130,6 @@ def add_swagger_ui(self):
console_ui_path)

for path in (
console_ui_path,
console_ui_path + '/',
console_ui_path + '/index.html',
):
Expand All @@ -128,8 +139,26 @@ def add_swagger_ui(self):
self._get_swagger_ui_home
)

# we have to add an explicit redirect instead of relying on the
# normalize_path_middleware because we also serve static files
# from this dir (below)

@asyncio.coroutine
def redirect(request):
raise web.HTTPMovedPermanently(
location=self.base_path + console_ui_path + '/'
)

self.subapp.router.add_route(
'GET',
console_ui_path,
redirect
)

# this route will match and get a permission error when trying to
# serve index.html, so we add the redirect above.
self.subapp.router.add_static(
console_ui_path + '/',
console_ui_path,
path=str(self.options.openapi_console_ui_from_dir),
name='swagger_ui_static'
)
Expand Down
8 changes: 5 additions & 3 deletions tests/aiohttp/test_aiohttp_simple_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def test_no_swagger_json(aiohttp_api_spec_dir, aiohttp_client):
specification_dir=aiohttp_api_spec_dir,
options=options,
debug=True)
api = app.add_api('swagger_simple.yaml')
app.add_api('swagger_simple.yaml')

app_client = yield from aiohttp_client(app.app)
swagger_json = yield from app_client.get('/v1.0/swagger.json') # type: flask.Response
Expand All @@ -102,7 +102,7 @@ def test_no_swagger_yaml(aiohttp_api_spec_dir, aiohttp_client):
specification_dir=aiohttp_api_spec_dir,
options=options,
debug=True)
api = app.add_api('swagger_simple.yaml')
app.add_api('swagger_simple.yaml')

app_client = yield from aiohttp_client(app.app)
spec_response = yield from app_client.get('/v1.0/swagger.yaml') # type: flask.Response
Expand All @@ -119,6 +119,7 @@ def test_swagger_ui(aiohttp_api_spec_dir, aiohttp_client):
app_client = yield from aiohttp_client(app.app)
swagger_ui = yield from app_client.get('/v1.0/ui')
assert swagger_ui.status == 200
assert swagger_ui.url.path == '/v1.0/ui/'
assert b'url = "/v1.0/swagger.json"' in (yield from swagger_ui.read())

swagger_ui = yield from app_client.get('/v1.0/ui/')
Expand Down Expand Up @@ -240,6 +241,7 @@ def test_validate_responses(aiohttp_app, aiohttp_client):
def test_get_users(aiohttp_client, aiohttp_app):
app_client = yield from aiohttp_client(aiohttp_app.app)
resp = yield from app_client.get('/v1.0/users')
assert resp.url.path == '/v1.0/users/' # followed redirect
assert resp.status == 200

json_data = yield from resp.json()
Expand All @@ -258,7 +260,7 @@ def test_create_user(aiohttp_client, aiohttp_app):
@asyncio.coroutine
def test_access_request_context(aiohttp_client, aiohttp_app):
app_client = yield from aiohttp_client(aiohttp_app.app)
resp = yield from app_client.post('/v1.0/aiohttp_access_request_context')
resp = yield from app_client.post('/v1.0/aiohttp_access_request_context/')
assert resp.status == 204


Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/aiohttp/swagger_simple.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ paths:
204:
description: success no content.

/users:
/users/:
get:
summary: Test get users
description: Get test users list
Expand Down