-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmir-openapi-generator.py
503 lines (453 loc) · 16.4 KB
/
mir-openapi-generator.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
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
import enum
import sys
import xml.sax
from xml.sax import ContentHandler
import yaml
def print_path(path):
print('\t' * 10, path['op'], path['path'])
print('Description:', path['desc'])
print('Parameters:')
for param in path['params']:
print('\ttype:', param['type'])
print('\tname:', param['name'])
print('\tdesc:', param['desc'])
print('\tschema:', param['schema'])
print('\t---')
print('Responses:')
for response in path['responses']:
print('\tcode:', response['code'])
print('\tdesc:', response['desc'])
print('\tschema:', response['schema'])
print('\t---')
print()
class PathsHandler(ContentHandler):
class State(enum.Enum):
DOC_START = enum.auto()
OP = enum.auto()
PATH = enum.auto()
DESC_START = enum.auto()
DESC = enum.auto()
PARAM_TYPE_HEADER = enum.auto()
PARAM_NAME_HEADER = enum.auto()
PARAM_DESC_HEADER = enum.auto()
PARAM_SCHEMA_HEADER = enum.auto()
PARAMS = enum.auto()
RESP_CODE_HEADER = enum.auto()
RESP_DESC_HEADER = enum.auto()
RESP_SCHEMA_HEADER = enum.auto()
RESPONSES = enum.auto()
TAGS = enum.auto()
class Word():
def __init__(self, xmin, ymin, content):
self.x = xmin
self.y = ymin
self.content = content
ParamTypes = ['Header', 'Path', 'Body']
FOOTER_MARGIN = 800
PARAM_MARGIN = 20
RESP_MARGIN = 20
def __init__(self):
self.skip = True
self.skip_word = 0
self.state = PathsHandler.State.DOC_START
self.paths = []
self.path = {}
self.attrs = None
def startElement(self, name, attrs):
if name == 'word':
self.attrs = attrs
self.skip = False
def endElement(self, name):
if name == 'word':
self.skip = True
def characters(self, content):
if self.skip:
return
if self.skip_word:
self.skip_word -= 1
return
if float(self.attrs['yMin']) > PathsHandler.FOOTER_MARGIN:
return
if self.state == PathsHandler.State.OP:
self.path = {}
self.path['op'] = content
self.state = PathsHandler.State.PATH
elif self.state == PathsHandler.State.PATH:
self.path['path'] = content
self.state = PathsHandler.State.DESC_START
elif self.state == PathsHandler.State.DESC_START:
if content == 'Description':
self.path['desc'] = ''
self.state = PathsHandler.State.DESC
elif self.state == PathsHandler.State.DESC:
if self.attrs['xMin'] == '48.240000' and content == 'Parameters':
self.path['desc'] = self.path['desc'].rstrip()
self.state = PathsHandler.State.PARAM_TYPE_HEADER
else:
self.path['desc'] += content + ' '
elif self.state == PathsHandler.State.PARAM_TYPE_HEADER:
self.param_type_xmin = float(self.attrs['xMin'])
self.state = PathsHandler.State.PARAM_NAME_HEADER
elif self.state == PathsHandler.State.PARAM_NAME_HEADER:
self.param_name_xmin = float(self.attrs['xMin'])
self.state = PathsHandler.State.PARAM_DESC_HEADER
elif self.state == PathsHandler.State.PARAM_DESC_HEADER:
self.param_desc_xmin = float(self.attrs['xMin'])
self.state = PathsHandler.State.PARAM_SCHEMA_HEADER
elif self.state == PathsHandler.State.PARAM_SCHEMA_HEADER:
self.param_schema_xmin = float(self.attrs['xMin'])
self.path['params'] = []
self.words = []
self.state = PathsHandler.State.PARAMS
elif self.state == PathsHandler.State.PARAMS:
if self.attrs['xMin'] == '48.240000' and content == 'Responses':
self.words.sort(key=lambda x: (x.y, x.x))
param = {}
param['type'] = ''
param['name'] = ''
param['desc'] = ''
param['schema'] = ''
last_y = self.words[0].y
for i in range(len(self.words)):
w = self.words[i]
if w.y - last_y > PathsHandler.PARAM_MARGIN:
param['desc'] = param['desc'].rstrip()
self.path['params'].append(param)
param = {}
param['type'] = ''
param['name'] = ''
param['desc'] = ''
param['schema'] = ''
if w.x >= self.param_type_xmin and w.x < self.param_name_xmin:
param['type'] += w.content
elif w.x >= self.param_name_xmin and w.x < self.param_desc_xmin:
if w.content == 'required':
param['required'] = True
else:
param['name'] += w.content
elif w.x >= self.param_desc_xmin and w.x < self.param_schema_xmin:
param['desc'] += w.content + ' '
else:
param['schema'] += w.content
last_y = w.y
param['desc'] = param['desc'].rstrip()
self.path['params'].append(param)
self.state = PathsHandler.State.RESP_CODE_HEADER
else:
xmin = float(self.attrs['xMin'])
ymin = float(self.attrs['yMin'])
self.words.append(PathsHandler.Word(xmin, ymin, content))
elif self.state == PathsHandler.State.RESP_CODE_HEADER:
self.resp_code_xmin = float(self.attrs['xMin'])
self.skip_word = 1
self.state = PathsHandler.State.RESP_DESC_HEADER
elif self.state == PathsHandler.State.RESP_DESC_HEADER:
self.resp_desc_xmin = float(self.attrs['xMin'])
self.state = PathsHandler.State.RESP_SCHEMA_HEADER
elif self.state == PathsHandler.State.RESP_SCHEMA_HEADER:
self.resp_schema_xmin = float(self.attrs['xMin'])
self.words = []
self.path['responses'] = []
self.state = PathsHandler.State.RESPONSES
elif self.state == PathsHandler.State.RESPONSES:
if self.attrs['xMin'] == '48.240000' and content == 'Tags':
self.words.sort(key=lambda x: (x.y, x.x))
resp = {}
resp['code'] = ''
resp['desc'] = ''
resp['schema'] = ''
last_y = self.words[0].y
for i in range(len(self.words)):
w = self.words[i]
if w.y - last_y > PathsHandler.PARAM_MARGIN:
resp['desc'] = resp['desc'].rstrip()
self.path['responses'].append(resp)
resp = {}
resp['code'] = ''
resp['desc'] = ''
resp['schema'] = ''
if w.x >= self.resp_code_xmin and w.x < self.resp_desc_xmin:
resp['code'] += w.content
elif w.x >= self.resp_desc_xmin and w.x < self.resp_schema_xmin:
resp['desc'] += w.content + ' '
else:
resp['schema'] += w.content
last_y = w.y
resp['desc'] = resp['desc'].rstrip()
self.path['responses'].append(resp)
self.paths.append(self.path)
self.state = PathsHandler.State.TAGS
else:
xmin = float(self.attrs['xMin'])
ymin = float(self.attrs['yMin'])
self.words.append(PathsHandler.Word(xmin, ymin, content))
elif self.state == PathsHandler.State.TAGS:
if self.attrs['xMin'] == '48.240000':
self.state = PathsHandler.State.OP
self.characters(content)
elif self.state == PathsHandler.State.DOC_START:
self.state = PathsHandler.State.OP
if content != 'Paths':
self.characters(content)
def print_definition(definition):
print(definition['name'])
print('=' * 3)
print('Properties:')
for prop in definition['properties']:
print('\tname:', prop['name'])
if len(prop['desc']):
print('\tdesc:', prop['desc'])
print('\tschema:', prop['schema'])
print()
print()
class DefinitionsHandler(ContentHandler):
class State(enum.Enum):
DOC_START = enum.auto()
DEFINITION_START = enum.auto()
PROP_NAME_HEADER = enum.auto()
PROP_DESC_HEADER = enum.auto()
PROP_SCHEMA_HEADER = enum.auto()
DEFINITION = enum.auto()
class Word():
def __init__(self, xmin, ymin, content):
self.x = xmin
self.y = ymin
self.content = content
FOOTER_MARGIN = 800
CONTINUATION_MARGIN = 42
PROP_MARGIN = 20
def __init__(self):
self.skip = True
self.skip_word = 0
self.state = DefinitionsHandler.State.DOC_START
self.attrs = None
self.definitions = []
self.prop = None
def startElement(self, name, attrs):
if name == 'word':
self.attrs = attrs
self.skip = False
def endElement(self, name):
if name == 'word':
self.skip = True
def endDocument(self):
if self.prop:
self.prop['desc'] = self.prop['desc'].rstrip()
self.definition['properties'].append(self.prop)
self.definitions.append(self.definition)
def characters(self, content):
if self.skip:
return
if self.skip_word:
self.skip_word -= 1
return
if float(self.attrs['yMin']) > DefinitionsHandler.FOOTER_MARGIN:
return
if self.state == DefinitionsHandler.State.DEFINITION_START:
self.definition = {}
self.definition['name'] = content
self.properties = {}
self.state = DefinitionsHandler.State.PROP_NAME_HEADER
elif self.state == DefinitionsHandler.State.PROP_NAME_HEADER:
if content == 'Type':
self.definition['properties'] = []
self.definitions.append(self.definition)
self.skip_word = 2
self.state = DefinitionsHandler.State.DEFINITION_START
else:
self.def_name_xmin = float(self.attrs['xMin'])
self.state = DefinitionsHandler.State.PROP_DESC_HEADER
elif self.state == DefinitionsHandler.State.PROP_DESC_HEADER:
if content == 'Schema':
self.def_has_desc = False
self.def_desc_xmin = float(self.attrs['xMin'])
self.state = DefinitionsHandler.State.PROP_SCHEMA_HEADER
self.characters(content)
else:
self.def_has_desc = True
self.def_desc_xmin = float(self.attrs['xMin'])
self.state = DefinitionsHandler.State.PROP_SCHEMA_HEADER
elif self.state == DefinitionsHandler.State.PROP_SCHEMA_HEADER:
self.def_schema_xmin = float(self.attrs['xMin'])
self.words = []
self.definition['properties'] = []
self.state = DefinitionsHandler.State.DEFINITION
elif self.state == DefinitionsHandler.State.DEFINITION:
if self.attrs['xMin'] == '48.240000':
self.prop = {}
self.prop['name'] = ''
self.prop['desc'] = ''
self.prop['schema'] = ''
last_y = self.words[0].y
for i in range(len(self.words)):
w = self.words[i]
if w.y < DefinitionsHandler.CONTINUATION_MARGIN:
if w.content == 'Name':
self.prop['desc'] = self.prop['desc'].rstrip()
self.definition['properties'].append(self.prop)
self.prop = {}
self.prop['name'] = ''
self.prop['desc'] = ''
self.prop['schema'] = ''
continue
if w.y - last_y > DefinitionsHandler.PROP_MARGIN:
self.prop['desc'] = self.prop['desc'].rstrip()
self.definition['properties'].append(self.prop)
self.prop = {}
self.prop['name'] = ''
self.prop['desc'] = ''
self.prop['schema'] = ''
self.process_prop_word(w, self.prop)
last_y = w.y
self.prop['desc'] = self.prop['desc'].rstrip()
self.definition['properties'].append(self.prop)
self.definitions.append(self.definition)
self.state = DefinitionsHandler.State.DEFINITION_START
self.characters(content)
else:
xmin = float(self.attrs['xMin'])
ymin = float(self.attrs['yMin'])
self.words.append(DefinitionsHandler.Word(xmin, ymin, content))
elif self.state == DefinitionsHandler.State.DOC_START:
self.state = DefinitionsHandler.State.DEFINITION_START
def process_prop_word(self, w, prop):
if w.x >= self.def_name_xmin and w.x < self.def_desc_xmin:
if w.content == 'optional':
prop['required'] = False
elif w.content == 'required':
prop['required'] = True
else:
prop['name'] += w.content
elif self.def_has_desc and w.x >= self.def_desc_xmin and w.x < self.def_schema_xmin:
prop['desc'] += w.content + ' '
else:
prop['schema'] += w.content
paths_handler = PathsHandler()
xml.sax.parse('mir_mir100_rest_api_270-paths.xml', paths_handler)
def_handler = DefinitionsHandler()
xml.sax.parse('mir_mir100_rest_api_270-schemas.xml', def_handler)
def to_oapi_schema(schema):
if schema.startswith('enum'):
return {'type': 'string'}
elif schema.startswith('string('):
start = schema.index('(') + 1
end = schema.index(')')
return {'type': 'string', 'format': f'{schema[start:end]}'}
elif schema == 'string' or schema == 'object' or schema == 'boolean':
return {'type': schema}
elif schema == 'integer':
return {'type': 'integer', 'format': 'int64'}
elif schema == 'integer(float)':
return {'type': 'integer', 'format': 'int64'}
elif schema == 'integer(int32)':
return {'type': 'integer', 'format': 'int32'}
elif schema == 'number(float)':
return {'type': 'number', 'format': 'float'}
elif schema == 'NoContent':
return {'type': 'null'}
elif schema.endswith('array'):
array_oapi = {}
array_oapi['type'] = 'array'
item_schema = schema[1:schema.index('>')]
array_oapi['items'] = to_oapi_schema(item_schema)
return array_oapi
else:
return {'$ref': f'#/components/schemas/{schema}'}
def is_no_content(schema):
if schema.get('type') == 'null':
return True
return False
root_oapi = {}
root_oapi['openapi'] = '3.0.1'
root_oapi['info'] = {}
info_oapi = root_oapi['info']
info_oapi['title'] = 'MIR100 Rest Interface'
info_oapi['description'] = 'Automatically converted from v270 pdf'
info_oapi['version'] = '2.7.0'
root_oapi['paths'] = {}
paths_oapi = root_oapi['paths']
for path in paths_handler.paths:
op_oapi = {}
op_oapi['summary'] = path['desc']
params_oapi = []
body_param = None
for param in path['params']:
# authorization is described in security schemes
if param['name'] == 'Authorization':
continue
param_oapi = {}
if param['type'] != 'Body':
param_oapi['in'] = param['type'].lower()
else:
body_param = param
continue
param_oapi['name'] = param['name']
param_oapi['description'] = param['desc']
if param.get('required'):
param_oapi['required'] = True
param_oapi['schema'] = to_oapi_schema(param['schema'])
# some post processing to make 'Accept-Language' default to 'en-US'
if param_oapi['name'] == 'Accept-Language':
del param_oapi['required']
param_oapi['schema']['default'] = 'en-US'
params_oapi.append(param_oapi)
op_oapi['parameters'] = params_oapi
if body_param:
body_oapi = {}
body_oapi['description'] = body_param['desc']
if body_param.get('required'):
body_oapi['required'] = True
content_oapi = {}
media_oapi = {}
media_oapi['schema'] = to_oapi_schema(body_param['schema'])
content_oapi['application/json'] = media_oapi
body_oapi['content'] = content_oapi
op_oapi['requestBody'] = body_oapi
resps_oapi = {}
for resp in path['responses']:
resp_oapi = {}
resp_oapi['description'] = resp['desc']
content_oapi = {}
media_oapi = {}
oapi_schema = to_oapi_schema(resp['schema'])
if not is_no_content(oapi_schema):
media_oapi['schema'] = to_oapi_schema(resp['schema'])
content_oapi['application/json'] = media_oapi
resp_oapi['content'] = content_oapi
resps_oapi[resp['code']] = resp_oapi
op_oapi['responses'] = resps_oapi
path_oapi = paths_oapi.setdefault(path['path'], {})
path_oapi[path['op'].lower()] = op_oapi
schemas_oapi = {}
for definition in def_handler.definitions:
schema_oapi = {}
schema_oapi['type'] = 'object'
props_oapi = {}
required = []
for prop in definition['properties']:
prop_oapi = {}
# parameters is wrongly set as string somethings in the pdf
if prop['name'] == 'parameters':
prop_oapi['type'] = 'array'
prop_oapi['items'] = {'type': 'object'}
else:
prop_oapi.update(to_oapi_schema(prop['schema']))
if len(prop['desc']):
prop_oapi['description'] = prop['desc']
props_oapi[prop['name']] = prop_oapi
if prop.get('required'):
required.append(prop['name'])
if len(required):
schema_oapi['required'] = required
schema_oapi['properties'] = props_oapi
schemas_oapi[definition['name']] = schema_oapi
root_oapi['components'] = {}
root_oapi['components']['schemas'] = schemas_oapi
# security scheme
root_oapi['components']['securitySchemes'] = {}
security_oapi = root_oapi['components']['securitySchemes']['mir'] = {}
security_oapi['type'] = 'http'
security_oapi['scheme'] = 'Basic'
security_oapi['description'] = 'username followed by a colon and the password sha-256 encoded. Ex: BASE64(\\<username\\>:SHA-256(\\<password\\>))'
root_oapi['security'] = [{'mir': []}]
print(yaml.dump(root_oapi))