aboutsummaryrefslogtreecommitdiff
path: root/tools/swagger2html.py
blob: 1121f04c904e5517d7ac207bfd6fa06c754441be (plain)
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/python

import sys, argparse, json, re

def handleException(etype, e=None):
    if etype == 'KeyError':
        print "Error: Required property {} not found".format(e)
    elif etype == 'IOError':
        print "Error ({}): {}".format(e.errno, e.strerror)
    elif etype == 'ValueError':
        print "Error: Unable to parse input as JSON"
    elif etype == 'Custom':
        print e
    sys.exit(1)

def get_json(filename):
    try:
        with open(filename) as json_file:
            json_data = json.load(json_file)
            return json_data
    except IOError as e:
        handleException('IOError', e)
    except ValueError:
        handleException('ValueError')
    except:
        print "Unexpected error: {}".format(sys.exc_info()[0])
        raise

def write_file(filename, body):
    try:
        with open(filename, 'w') as md_file:
            md_file.write(body)
    except IOError as e:
        handleException('IOError', e)

def make_header(json_data):
    try:
        if not 'swagger' in json_data:
            raise KeyError
        info = json_data['info']
        md = "<h1>{}</h1>\n".format(info['title'])
        md += "<p>Version: {}</p>\n".format(info['version'])
        if 'license' in info:
            md += "<p>License: "
            license = info['license']
            if 'url' in license:
                md += '<a href="{}">{}</a>'.format(license['url'], license['name'])
            else:
                md += license['name']
            md += '</p>\n'
        if 'contact' in info:
            contact = info['contact']
            if 'name' in contact or 'email' in contact:
                md += '<p>Contact: '
                if not 'name' in contact:
                    md += '<a href="mailto:{0}">{0}</a>'.format(contact['email'])
                elif not 'email' in contact:
                    md += contact['name']
                else:
                    md += '{0} &lt;<a href="mailto:{1}"&gt;{1}</a>'.format(contact['name'], contact['email'])
                md += '  \n'
            if 'url' in contact:
                md += "<p>Website: {}</p>\n".format(contact['url'])
        if 'termsOfService' in info:
            md += '<p>Terms of Service: {}</p>\n'.format(info['termsOfService'])
        if 'host' in json_data:
            md += '<p>Base URL: '
            base = json_data['host']
            if 'basePath' in json_data:
                base += json_data['basePath']
            else:
                base += '/'
            if 'schemes' in json_data:
                md += (', ').join(map(
                    lambda x: '<a href="{0}://{1}">{0}://{1}</a>'.format(x, base),
                    json_data['schemes']
                ))
            else:
                md += '<a href="{0}">{0}</a>'.format(base)
            md += '</p>\n'
        if 'description' in info:
            md += '<p>Description: {}</p>\n'.format(info['description'])
        md += '\n'
        return md
    except KeyErrori as e:
        handleException('KeyError', e)

def make_ref(ref):
    href = ref.split('/')[1:]
    return '<a href="#{}">{}</a>'.format('_'.join(href), href[-1])

def get_ref(ref, raw):
    keys = ref.split('/')
    return raw[keys[1]][keys[2]]

def make_html(s):
    reg = re.compile(r"[*_]{3}(.+?)[*_]{3}")
    s = reg.sub(r"<strong><em>\1</em></strong>", s)
    reg = re.compile(r"[*_]{2}(.+?)[*_]{2}")
    s = reg.sub(r"<strong>\1</strong>", s)
    reg = re.compile(r"[*_](.+?)[*_]")
    s = reg.sub(r"<em>\1</em>", s)
    reg = re.compile(r"\`(.+?)\`")
    s = reg.sub(r"<code>\1</code>", s)
    return s

def make_table(data):
    md = '<table class="table-bordered">\n'
    md += '  <thead>\n'
    md += '    <tr>\n'
    for col in data[0]:
        md += '      <td>{}</td>\n'.format(col)
    md += '    </tr>\n'
    md += '  </thead>\n'
    md += '  <tbody>\n'
    for row in data[1:]:
        md += '    <tr>\n'
        for cell in row:
            md += '      <td>{}</td>\n'.format(cell)
        md += '    </tr>\n'
    md += '  </tbody>\n'
    md += '</table>\n'
    return md

def make_params_table(itemsraw, raw):
    items = []
    for item in itemsraw:
        if '$ref' in item:
            items.append(get_ref(item['$ref'], raw))
        else:
            items.append(item)
    try:
        fields = list(set([]).union(*map(lambda x: x.keys(), items)))
        row = [ 'Name', 'ParamType' ]
        if 'description' in fields:
            row.append('Description')
        if 'required' in fields:
            row.append('Required')
        if 'type' in fields:
            row.append('DataType')
        if 'schema' in fields:
            row.append('Schema')
        table = [ row ]
        for item in items:
            row = [ "<em>{}</em>".format(item['name']), item['in'] ]
            if 'description' in fields:
                if 'description' in item:
                    row.append(make_html(item['description']))
                else:
                    row.append('')
            if 'required' in fields:
                required = 'False'
                if 'required' in item and item['required']:
                    required = "<strong>True</strong>"
                row.append(required)
            if 'type' in fields:
                type = ''
                if 'type' in item:
                    if item['type'] == 'array':
                        type = "[ <em>{}</em> ]".format(item['items']['type'])
                    else:
                        type = item['type']
                        if 'format' in item:
                            type += " ({})".format(item['format'])
                        type = "<em>{}</em>".format(type)
                row.append(type)
            if 'schema' in fields:
                if 'schema' in item:
                    if '$ref' in item['schema']:
                        row.append(make_ref(item['schema']['$ref']))
                else:
                    row.append('')
            table.append(row)
        return make_table(table)
    except KeyError as e:
        handleException('KeyError', e)

def make_responses_table(responses):
    try:
        fields = list(
            set([]).union(*map(lambda x: x.keys(),
                map(lambda x: responses[x], responses.keys())
            ))
        )
        row = [ 'Status Code', 'Description' ]
        if 'headers' in fields:
            row.append('Headers')
        if 'schema' in fields:
            row.append('Schema')
        if 'examples' in fields:
            row.append('Examples')
        table = [ row ]
        for key in sorted(responses):
            response = responses[key]
            row = [ "<em>{}</em>".format(key), make_html(response['description']) ]
            if 'headers' in fields:
                header = ''
                if 'headers' in response:
                    hrow = []
                    for header, h_obj in response['headers'].iteritems():
                        hrow += "{} ({})".format(header, h_obj['type'])
                        if 'description' in h_obj:
                            hrow += ": {}".format(h_obj['description'])
                    header = '  \n'.join(hrow)
                row.append(header)
            if 'schema' in fields:
                schema = ''
                if 'schema' in response:
                    if '$ref' in response['schema']:
                        schema += make_ref(response['schema']['$ref'])
                    if 'type' in response['schema']:
                        if response['schema']['type'] == 'array':
                            if '$ref' in response['schema']['items']:
                                schema += make_ref(response['schema']['items']['$ref'])
                            schema = "[ {} ]".format(schema)
                row.append(schema)
            if 'examples' in fields:
                if 'examples' in response:
                    row.append(response['examples'])
                else:
                    row.append('')
            table.append(row)
        return make_table(table)
    except KeyError as e:
        handleException('KeyError', e)

def sorted_by_method(section):
    sorting_function = lambda x: [ 'GET', 'POST', 'PUT', 'DELETE' ].index(
        x['title'].split(' ')[0]
    )
    return sorted(sorted(section), key=sorting_function)

def make_paths(sections, json_data):
    md = '<h2><a name="paths"></a>Paths</h2>\n'
    for key in sorted(sections):
        md += '<h3><a name="paths_{0}"></a>{0}</h3>\n'.format(key)
        for section in sorted_by_method(sections[key]):
            md += '<h4><a name="{}"></a><code>{}</code></h4>\n'.format(
                section['href'], section['title']
            )
            operation = section['operation']
            if 'summary' in operation:
                md += '<p>Summary: {}</p>\n'.format(make_html(operation['summary']))
            if 'description' in operation:
                md += '<p>Description: {}</p>\n'.format(make_html(operation['description']))
            md += '<h5>Parameters</h5>\n'
            if 'parameters' in operation and len(operation['parameters']) > 0:
                md += make_params_table(operation['parameters'], json_data)
            else:
                md += "<p><em>None</em></p>\n"
            md += '<h5>Responses</h5>\n'
            md += make_responses_table(operation['responses'])
        md += '\n'
    md += '\n'
    return md

def make_contents(path_section, json_data):
    md = '<h3>Contents</h3>\n'
    md += '<ul>\n'
    md += '  <li><a href="#paths">Paths</a>\n'
    md += '    <ul>\n'
    for key in sorted(path_section):
        md += '      <li><a href="#paths_{0}">{0}</a>\n'.format(key)
        md += '        <ul>\n'
        for section in sorted_by_method(path_section[key]):
            md += '          <li><a href="#{}">{}</a></li>\n'.format(
                section['href'], section['title']
            )
        md += '        </ul>\n'
        md += '      </li>\n'
    md += '    </ul>\n'
    md += '  </li>\n'
    md += '  <li><a href="#definitions">Models</a>\n'
    md += '    <ul>\n'
    for key in sorted(json_data['definitions']):
        md += '      <li><a href="#definitions_{0}">{0}</a></li>\n'.format(key)
    md += '    </ul>\n'
    md += '  </li>\n'
    md += '</ul>\n'
    return md

def make_definitions(json_data):
    md = '<h2><a name="definitions"></a>Models</h2>\n'
    for name in sorted(json_data['definitions']):
        md += '<h3><a name="definitions_{0}"></a>{0}</h3>\n'.format(name)
        model = json_data['definitions'][name]
        if 'properties' in model:
            fields = list(
                set(['type']).union(
                    *map(lambda x: x.keys(),
                        map(lambda x: model['properties'][x], model['properties'].keys())
                    )
                )
            )
            row = [ 'Property', 'Type' ]
            if 'description' in fields:
                row.append('Description')
            table = [ row ]
            for key in sorted(model['properties']):
                property = model['properties'][key]
                row = [ "<em>{}</em>".format(key) ]
                if 'type' in property:
                    type = property['type']
                    if 'format' in property:
                        type += " ({})".format(property['format'])
                    row.append("<em>{}</em>".format(type))
                elif '$ref' in property:
                    row.append(make_ref(property['$ref']))
                else:
                    row.append('')
                if 'description' in fields:
                    if 'description' in property:
                        row.append(make_html(property['description']))
                    else:
                        row.append('')
                table.append(row)
        md += make_table(table)
    return md

def make_markdown(json_data):
    path_sections = {}
    for endpoint in json_data['paths']:
        path_split = endpoint.split('/')
        path_key = path_split[1]
        if not path_key in path_sections:
            path_sections[path_key] = []
        for method, operation in json_data['paths'][endpoint].iteritems():
            if 'operationId' in operation:
                link = operation['operationId']
            else:
                link = ''.join([
                    c for c in endpoint if c not in ['/', '{', '}']
                ])
            path_sections[path_key].append({
                'title': '{} {}'.format(method.upper(), endpoint),
                'href': 'paths_{}_{}'.format(link, method.upper()),
                'operation': operation
            })
    md = make_header(json_data)
    md += make_contents(path_sections, json_data)
    md += make_paths(path_sections, json_data)
    md += make_definitions(json_data)
    return md

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('SPECFILE', help="path to swagger.json file")
    parser.add_argument('OUTFILE', help="path to output HTML file")
    args = parser.parse_args()

    marked_down = make_markdown(get_json(args.SPECFILE))

    if args.OUTFILE:
        write_file(args.OUTFILE, marked_down)
        print " success: {}".format(args.OUTFILE)
    else:
        print marked_down

if __name__ == '__main__':
    main()