-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_openapi.py
72 lines (61 loc) · 3.8 KB
/
extract_openapi.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
# python3 extract_openapi.py /mnt/c/LocalGitRepos/stackql/openapi-conversion/openapisaurus/dev/confluent/v00.00.00000/services
# python3 extract_openapi.py /mnt/c/LocalGitRepos/stackql/openapi-conversion/openapisaurus/dev/entra/v00.00.00000/services
import os
import yaml
import csv
def extract_openapi_details(yaml_dir):
output_file = 'output.csv'
with open(output_file, 'w', newline='') as csvfile:
fieldnames = ['service', 'operationId', 'path', 'verb', 'tags', 'summary', 'respSchema']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for root, _, files in os.walk(yaml_dir):
for file in files:
if file.endswith('.yaml') and not file.endswith('-resources.yaml'):
service_name = file.rsplit('.', 1)[0]
yaml_path = os.path.join(root, file)
with open(yaml_path, 'r') as f:
try:
openapi_spec = yaml.safe_load(f)
except yaml.YAMLError as exc:
print(f"Error parsing {yaml_path}: {exc}")
continue
paths = openapi_spec.get('paths', {})
for path, path_item in paths.items():
for verb, operation in path_item.items():
if verb not in ['get', 'post', 'put', 'delete', 'patch', 'options', 'head']:
continue
operation_id = operation.get('operationId', '')
tags = operation.get('tags', [])
tags_str = '|'.join(tags)
summary = operation.get('summary', '')
resp_schema = ''
# Check responses in the desired priority order
responses = operation.get('responses', {})
for status in ['2XX', '200'] + [f'2{str(i)}' for i in range(10)] + ['default']:
if status in responses:
response = responses[status]
# Check if $ref is directly in the response
if '$ref' in response:
resp_schema = response['$ref'].replace('#/components/responses/', '')
break
# Check in content => application/json => schema => $ref
content = response.get('content', {}).get('application/json', {}).get('schema', {})
if '$ref' in content:
resp_schema = content['$ref'].replace('#/components/schemas/', '')
break
writer.writerow({
'service': service_name,
'operationId': operation_id,
'path': path,
'verb': verb,
'tags': tags_str,
'summary': summary,
'respSchema': resp_schema
})
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser(description='Extract OpenAPI details from YAML files.')
parser.add_argument('yaml_dir', type=str, help='Directory containing YAML files')
args = parser.parse_args()
extract_openapi_details(args.yaml_dir)