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

Account for SCRIPT_NAME #125

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions flask_apispec/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,22 @@ def add_swagger_routes(self):

self.app.register_blueprint(blueprint)

def fix_base_path(self):
"""Lazily set OpenAPI basePath for each request.

This is to reflect the path root the application is running under.
c.f. SCRIPT_NAME https://www.python.org/dev/peps/pep-0333/#environ-variables
"""
if not flask.has_request_context():
# not in request
return
if not flask.request.script_root:
# not running under different root
return
self.spec.options['basePath'] = flask.request.script_root

def swagger_json(self):
self.fix_base_path()
return flask.jsonify(self.spec.to_dict())

def swagger_ui(self):
Expand Down
12 changes: 5 additions & 7 deletions flask_apispec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ def __init__(self, options=None, inherit=None, apply=None):

def __eq__(self, other):
if isinstance(other, Annotation):
return (
self.options == other.options and
self.inherit == other.inherit and
return self.options == other.options and \
self.inherit == other.inherit and \
self.apply == other.apply
)

return NotImplemented

def __ne__(self, other):
Expand All @@ -76,10 +75,9 @@ def merge(self, other):
)

def resolve_annotations(func, key, parent=None):
annotations = (
getattr(func, '__apispec__', {}).get(key, []) +
annotations = getattr(func, '__apispec__', {}).get(key, []) + \
getattr(parent, '__apispec__', {}).get(key, [])
)

return functools.reduce(
lambda first, second: first.merge(second),
[annotation.resolve(parent) for annotation in annotations],
Expand Down