From f8c8cc93f812f054547aab5537c0a2cb02d72a64 Mon Sep 17 00:00:00 2001 From: SlimaK Date: Mon, 26 Mar 2018 23:18:31 +0200 Subject: [PATCH 01/10] Added basic_path parameter --- flask_swagger.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/flask_swagger.py b/flask_swagger.py index 352e201..488a13a 100644 --- a/flask_swagger.py +++ b/flask_swagger.py @@ -5,10 +5,12 @@ If a swagger yaml description is found in the docstrings for an endpoint we add the endpoint to swagger specification output +- Added basic_path parameter """ import inspect import yaml import re +import os from collections import defaultdict @@ -17,7 +19,7 @@ def _sanitize(comment): return comment.replace('\n', '
') if comment else comment -def _find_from_file(full_doc, from_file_keyword): +def _find_from_file(full_doc, from_file_keyword, basic_path): """ Finds a line in like @@ -33,7 +35,7 @@ def _find_from_file(full_doc, from_file_keyword): if len(parts) == 2 and parts[0].strip() == from_file_keyword: path = parts[1].strip() break - + path = path if not path else os.path.join(basic_path, path) return path @@ -44,12 +46,12 @@ def _doc_from_file(path): return doc -def _parse_docstring(obj, process_doc, from_file_keyword): +def _parse_docstring(obj, process_doc, from_file_keyword, basic_path): first_line, other_lines, swag = None, None, None full_doc = inspect.getdoc(obj) if full_doc: if from_file_keyword is not None: - from_file = _find_from_file(full_doc, from_file_keyword) + from_file = _find_from_file(full_doc, from_file_keyword, basic_path) if from_file: full_doc_from_file = _doc_from_file(from_file) if full_doc_from_file: @@ -122,7 +124,7 @@ def _extract_array_defs(source): def swagger(app, prefix=None, process_doc=_sanitize, - from_file_keyword=None, template=None): + from_file_keyword=None, template=None, basic_path=""): """ Call this from an @app.route method like this @app.route('/spec.json') @@ -171,17 +173,15 @@ def spec(): endpoint = app.view_functions[rule.endpoint] methods = dict() for verb in rule.methods.difference(ignore_verbs): - verb = verb.lower() - if hasattr(endpoint, 'methods') \ - and verb in map(lambda m: m.lower(), endpoint.methods) \ - and hasattr(endpoint.view_class, verb): + if hasattr(endpoint, 'methods') and verb in endpoint.methods: + verb = verb.lower() methods[verb] = getattr(endpoint.view_class, verb) else: - methods[verb] = endpoint + methods[verb.lower()] = endpoint operations = dict() for verb, method in methods.items(): summary, description, swag = _parse_docstring(method, process_doc, - from_file_keyword) + from_file_keyword, basic_path) if swag is not None: # we only add endpoints with swagger data in the docstrings defs = swag.get('definitions', []) defs = _extract_definitions(defs) From 300369906bad12dd7879b801654e6476413b3d07 Mon Sep 17 00:00:00 2001 From: SlimaK Date: Mon, 26 Mar 2018 23:27:31 +0200 Subject: [PATCH 02/10] Update README.md --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c36b1b9..9ba135f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,24 @@ -# flask-swagger +# flask-swagger (fork) A Swagger 2.0 spec extractor for Flask +Using this fork you can now specify base path for yml files: +``` +app = Flask(__name__) + +@app.route("/spec") +def spec(): + base_path = os.path.join(app.root_path, 'docs') + return jsonify(swagger(app), from_file_keyword="swagger_from_file", base_path=base_path) +``` +and use relative pathes: +``` +@app.route('/test', methods=['POST']) +def login(): + """ + swagger_from_file: test.yml + """ +``` + Install: ```shell From fe10c1c6be3610eaea9a57a1d0e734b9dcd0bdd2 Mon Sep 17 00:00:00 2001 From: SlimaK Date: Mon, 26 Mar 2018 23:29:22 +0200 Subject: [PATCH 03/10] Renamed basic_path to base_path --- flask_swagger.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/flask_swagger.py b/flask_swagger.py index 488a13a..7cc3537 100644 --- a/flask_swagger.py +++ b/flask_swagger.py @@ -19,7 +19,7 @@ def _sanitize(comment): return comment.replace('\n', '
') if comment else comment -def _find_from_file(full_doc, from_file_keyword, basic_path): +def _find_from_file(full_doc, from_file_keyword, base_path): """ Finds a line in like @@ -35,7 +35,7 @@ def _find_from_file(full_doc, from_file_keyword, basic_path): if len(parts) == 2 and parts[0].strip() == from_file_keyword: path = parts[1].strip() break - path = path if not path else os.path.join(basic_path, path) + path = path if not path else os.path.join(base_path, path) return path @@ -46,12 +46,12 @@ def _doc_from_file(path): return doc -def _parse_docstring(obj, process_doc, from_file_keyword, basic_path): +def _parse_docstring(obj, process_doc, from_file_keyword, base_path): first_line, other_lines, swag = None, None, None full_doc = inspect.getdoc(obj) if full_doc: if from_file_keyword is not None: - from_file = _find_from_file(full_doc, from_file_keyword, basic_path) + from_file = _find_from_file(full_doc, from_file_keyword, base_path) if from_file: full_doc_from_file = _doc_from_file(from_file) if full_doc_from_file: @@ -124,7 +124,7 @@ def _extract_array_defs(source): def swagger(app, prefix=None, process_doc=_sanitize, - from_file_keyword=None, template=None, basic_path=""): + from_file_keyword=None, template=None, base_path=""): """ Call this from an @app.route method like this @app.route('/spec.json') @@ -181,7 +181,7 @@ def spec(): operations = dict() for verb, method in methods.items(): summary, description, swag = _parse_docstring(method, process_doc, - from_file_keyword, basic_path) + from_file_keyword, base_path) if swag is not None: # we only add endpoints with swagger data in the docstrings defs = swag.get('definitions', []) defs = _extract_definitions(defs) From b589497f888ab59f7ef1deb507bc41ebc34d44d1 Mon Sep 17 00:00:00 2001 From: SlimaK Date: Mon, 26 Mar 2018 23:56:02 +0200 Subject: [PATCH 04/10] Fixed typo --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9ba135f..e310681 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ def spec(): base_path = os.path.join(app.root_path, 'docs') return jsonify(swagger(app), from_file_keyword="swagger_from_file", base_path=base_path) ``` -and use relative pathes: +and use relative paths: ``` @app.route('/test', methods=['POST']) def login(): From f1dbf918d9903a588eed3cce2a87eeccc9f8cc0e Mon Sep 17 00:00:00 2001 From: Steven Eardley Date: Wed, 2 May 2018 14:11:51 +0100 Subject: [PATCH 05/10] added doaj customisations --- flask_swagger.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/flask_swagger.py b/flask_swagger.py index 352e201..080274c 100644 --- a/flask_swagger.py +++ b/flask_swagger.py @@ -182,6 +182,12 @@ def spec(): for verb, method in methods.items(): summary, description, swag = _parse_docstring(method, process_doc, from_file_keyword) + if hasattr(method, 'summary'): + summary = method.summary + if hasattr(method, 'description'): + description = method.description + if hasattr(method, 'swag'): + swag = method.swag if swag is not None: # we only add endpoints with swagger data in the docstrings defs = swag.get('definitions', []) defs = _extract_definitions(defs) From c1fe8ca2980c5b226031adef877d52b5be142f04 Mon Sep 17 00:00:00 2001 From: Kamil Date: Tue, 6 Aug 2019 15:45:54 +0200 Subject: [PATCH 06/10] Fixed merge conflicts --- flask_swagger.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/flask_swagger.py b/flask_swagger.py index 1e81b45..7cc3537 100644 --- a/flask_swagger.py +++ b/flask_swagger.py @@ -181,17 +181,7 @@ def spec(): operations = dict() for verb, method in methods.items(): summary, description, swag = _parse_docstring(method, process_doc, -<<<<<<< HEAD from_file_keyword, base_path) -======= - from_file_keyword) - if hasattr(method, 'summary'): - summary = method.summary - if hasattr(method, 'description'): - description = method.description - if hasattr(method, 'swag'): - swag = method.swag ->>>>>>> f1dbf918d9903a588eed3cce2a87eeccc9f8cc0e if swag is not None: # we only add endpoints with swagger data in the docstrings defs = swag.get('definitions', []) defs = _extract_definitions(defs) From 42c066fc41c889f082a955f04b8905e628847e2b Mon Sep 17 00:00:00 2001 From: Kamil Date: Tue, 6 Aug 2019 17:56:58 +0200 Subject: [PATCH 07/10] Fixed typo --- flask_swagger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_swagger.py b/flask_swagger.py index 7cc3537..d14b200 100644 --- a/flask_swagger.py +++ b/flask_swagger.py @@ -173,8 +173,8 @@ def spec(): endpoint = app.view_functions[rule.endpoint] methods = dict() for verb in rule.methods.difference(ignore_verbs): + verb = verb.lower() if hasattr(endpoint, 'methods') and verb in endpoint.methods: - verb = verb.lower() methods[verb] = getattr(endpoint.view_class, verb) else: methods[verb.lower()] = endpoint From 5db932ef80210e3582504e29189a0dc24dba5bfb Mon Sep 17 00:00:00 2001 From: Kamil Date: Tue, 6 Aug 2019 17:59:14 +0200 Subject: [PATCH 08/10] Removed world "fork" --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e310681..f537000 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -# flask-swagger (fork) +# flask-swagger A Swagger 2.0 spec extractor for Flask -Using this fork you can now specify base path for yml files: +You can now specify base path for yml files: ``` app = Flask(__name__) From 29afb75ddc37d14f2b80be7f14b0300dfa3da9d9 Mon Sep 17 00:00:00 2001 From: Kamil Date: Tue, 6 Aug 2019 18:01:55 +0200 Subject: [PATCH 09/10] Removed redundant method call --- flask_swagger.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask_swagger.py b/flask_swagger.py index d14b200..1a466aa 100644 --- a/flask_swagger.py +++ b/flask_swagger.py @@ -177,7 +177,7 @@ def spec(): if hasattr(endpoint, 'methods') and verb in endpoint.methods: methods[verb] = getattr(endpoint.view_class, verb) else: - methods[verb.lower()] = endpoint + methods[verb] = endpoint operations = dict() for verb, method in methods.items(): summary, description, swag = _parse_docstring(method, process_doc, From d686a31400d84ca538156581d625dc0a0ae77d72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Sowi=C5=84ski?= Date: Fri, 9 Aug 2019 16:16:39 +0200 Subject: [PATCH 10/10] Reverted endpoints loading --- flask_swagger.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/flask_swagger.py b/flask_swagger.py index 1a466aa..71830e9 100644 --- a/flask_swagger.py +++ b/flask_swagger.py @@ -174,7 +174,9 @@ def spec(): methods = dict() for verb in rule.methods.difference(ignore_verbs): verb = verb.lower() - if hasattr(endpoint, 'methods') and verb in endpoint.methods: + if hasattr(endpoint, 'methods') \ + and verb in map(lambda m: m.lower(), endpoint.methods) \ + and hasattr(endpoint.view_class, verb): methods[verb] = getattr(endpoint.view_class, verb) else: methods[verb] = endpoint