-
Notifications
You must be signed in to change notification settings - Fork 157
/
fix-spec.py
executable file
·46 lines (37 loc) · 1.66 KB
/
fix-spec.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
#!/usr/bin/env python3
import yaml
SPEC_PATH = 'api/openapi.yaml'
# Load the spec file
with open(SPEC_PATH, 'r') as file:
data = yaml.load(file, Loader=yaml.CLoader)
# Traverse schemas
if 'components' in data and 'schemas' in data['components']:
for name, schema in data['components']['schemas'].items():
if 'properties' in schema:
# Remove "null" item from nullable enums
for name, prop in schema['properties'].items():
if 'enum' in prop and None in prop['enum']:
prop['enum'].remove(None)
if 'properties' in prop and 'value' in prop['properties'] and 'enum' in prop['properties']['value'] and None in prop['properties']['value']['enum']:
prop['properties']['value']['enum'].remove(None)
# Fix nullable types
nullable_types = [
'parent_device',
'primary_ip',
]
for ntype in nullable_types:
if ntype in schema['properties']:
schema['properties'][ntype]['nullable'] = True
# Fix non-nullable types
# See: https://github.com/OpenAPITools/openapi-generator/issues/18006
non_nullable_types = [
'front_image',
'rear_image',
]
for ntype in non_nullable_types:
if ntype in schema['properties']:
if schema['properties'][ntype]['format'] == 'binary':
schema['properties'][ntype].pop('nullable')
# Save the spec file
with open(SPEC_PATH, 'w') as file:
yaml.dump(data, file, Dumper=yaml.CDumper, sort_keys=False)