diff --git a/flask_apispec/extension.py b/flask_apispec/extension.py index 2c6bbb5..5767682 100644 --- a/flask_apispec/extension.py +++ b/flask_apispec/extension.py @@ -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): diff --git a/flask_apispec/utils.py b/flask_apispec/utils.py index 8c66e35..e6b5a27 100644 --- a/flask_apispec/utils.py +++ b/flask_apispec/utils.py @@ -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): @@ -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],