From 2fff3d58788f4558f67107c79b22f8566b3c2936 Mon Sep 17 00:00:00 2001 From: Mischa Spiegelmock Date: Sat, 10 Nov 2018 10:15:49 +0200 Subject: [PATCH 1/2] Account for SCRIPT_NAME when generating documentation if application is not hosted on / --- flask_apispec/extension.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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): From 7cf50d33189873f498b4474610e32896247a1260 Mon Sep 17 00:00:00 2001 From: Mischa Spiegelmock Date: Fri, 16 Nov 2018 14:25:56 +0200 Subject: [PATCH 2/2] ridiculous lint --- flask_apispec/utils.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) 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],