forked from getsling/flask-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_swagger.py
226 lines (193 loc) · 7.86 KB
/
flask_swagger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
What's the big idea?
An endpoint that traverses all restful endpoints producing a swagger 2.0 schema
If a swagger yaml description is found in the docstrings for an endpoint
we add the endpoint to swagger specification output
"""
import inspect
import yaml
import re
from collections import defaultdict
def _sanitize(comment):
return comment.replace('\n', '<br/>') if comment else comment
def _find_from_file(full_doc, from_file_keyword):
"""
Finds a line in <full_doc> like
<from_file_keyword> <colon> <path>
and return path
"""
path = None
for line in full_doc.splitlines():
if from_file_keyword in line:
parts = line.strip().split(':')
if len(parts) == 2 and parts[0].strip() == from_file_keyword:
path = parts[1].strip()
break
return path
def _doc_from_file(path):
doc = None
with open(path) as f:
doc = f.read()
return doc
def _parse_docstring(obj, process_doc, from_file_keyword):
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)
if from_file:
full_doc_from_file = _doc_from_file(from_file)
if full_doc_from_file:
full_doc = full_doc_from_file
line_feed = full_doc.find('\n')
if line_feed != -1:
first_line = process_doc(full_doc[:line_feed])
yaml_sep = full_doc[line_feed+1:].find('---')
if yaml_sep != -1:
other_lines = process_doc(full_doc[line_feed+1:line_feed+yaml_sep])
swag = yaml.load(full_doc[line_feed+yaml_sep:])
else:
other_lines = process_doc(full_doc[line_feed+1:])
else:
first_line = full_doc
return first_line, other_lines, swag
def _extract_definitions(alist, level=None):
"""
Since we couldn't be bothered to register models elsewhere
our definitions need to be extracted from the parameters.
We require an 'id' field for the schema to be correctly
added to the definitions list.
"""
def _extract_array_defs(source):
# extract any definitions that are within arrays
# this occurs recursively
ret = []
items = source.get('items')
if items is not None and 'schema' in items:
ret += _extract_definitions([items], level+1)
return ret
# for tracking level of recursion
if level is None:
level = 0
defs = list()
if alist is not None:
for item in alist:
schema = item.get("schema")
if schema is not None:
schema_id = schema.get("id")
if schema_id is not None:
defs.append(schema)
ref = {"$ref": "#/definitions/{}".format(schema_id)}
# only add the reference as a schema if we are in a response or
# a parameter i.e. at the top level
# directly ref if a definition is used within another definition
if level == 0:
item['schema'] = ref
else:
item.update(ref)
del item['schema']
# extract any definitions that are within properties
# this occurs recursively
properties = schema.get('properties')
if properties is not None:
defs += _extract_definitions(properties.values(), level+1)
defs += _extract_array_defs(schema)
defs += _extract_array_defs(item)
return defs
def swagger(app, prefix=None, process_doc=_sanitize,
from_file_keyword=None, template=None):
"""
Call this from an @app.route method like this
@app.route('/spec.json')
def spec():
return jsonify(swagger(app))
We go through all endpoints of the app searching for swagger endpoints
We provide the minimum required data according to swagger specs
Callers can and should add and override at will
Arguments:
app -- the flask app to inspect
Keyword arguments:
process_doc -- text sanitization method, the default simply replaces \n with <br>
from_file_keyword -- how to specify a file to load doc from
template -- The spec to start with and update as flask-swagger finds paths.
"""
output = {
"swagger": "2.0",
"info": {
"version": "0.0.0",
"title": "Cool product name",
}
}
paths = defaultdict(dict)
definitions = defaultdict(dict)
if template is not None:
output.update(template)
# check for template provided paths and definitions
for k, v in output.get('paths', {}).items():
paths[k] = v
for k, v in output.get('definitions', {}).items():
definitions[k] = v
output["paths"] = paths
output["definitions"] = definitions
ignore_verbs = {"HEAD", "OPTIONS"}
# technically only responses is non-optional
optional_fields = ['tags', 'consumes', 'produces', 'schemes', 'security',
'deprecated', 'operationId', 'externalDocs']
for rule in app.url_map.iter_rules():
if prefix and rule.rule[:len(prefix)] != prefix:
continue
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):
methods[verb] = getattr(endpoint.view_class, verb)
else:
methods[verb] = endpoint
operations = dict()
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)
params = swag.get('parameters', [])
defs += _extract_definitions(params)
responses = swag.get('responses', {})
responses = {
str(key): value
for key, value in responses.items()
}
if responses is not None:
defs = defs + _extract_definitions(responses.values())
for definition in defs:
def_id = definition.pop('id')
if def_id is not None:
definitions[def_id].update(definition)
operation = dict(
summary=summary,
description=description,
responses=responses
)
# parameters - swagger ui dislikes empty parameter lists
if len(params) > 0:
operation['parameters'] = params
# other optionals
for key in optional_fields:
if key in swag:
operation[key] = swag.get(key)
operations[verb] = operation
if len(operations):
rule = str(rule)
for arg in re.findall('(<([^<>]*:)?([^<>]*)>)', rule):
rule = rule.replace(arg[0], '{%s}' % arg[2])
paths[rule].update(operations)
return output