-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathopenapi-schema-builder.py
More file actions
274 lines (233 loc) · 11.1 KB
/
Copy pathopenapi-schema-builder.py
File metadata and controls
274 lines (233 loc) · 11.1 KB
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
import json
import sys
import argparse
from openapi_spec_validator import validate_spec
from urllib.parse import urlparse
from termcolor import colored
import time
import os
# ASCII Art
# This script will allow you to convert postman collection into openapi schema required by burp suite
# Fetched from https://github.com/dr34mhacks/Openapi-Schema-Builder
ASCII_ART = """
____ ___ ____ ____ _____ __ ____ _ __ __
/ __ \____ ___ ____ / | / __ \/ _/ / ___/_____/ /_ ___ ____ ___ ____ _ / __ )__ __(_) /___/ /__ _____
/ / / / __ \/ _ \/ __ \/ /| | / /_/ // /_____\__ \/ ___/ __ \/ _ \/ __ `__ \/ __ `/_____/ __ / / / / / / __ / _ \/ ___/
/ /_/ / /_/ / __/ / / / ___ |/ ____// /_____/__/ / /__/ / / / __/ / / / / / /_/ /_____/ /_/ / /_/ / / / /_/ / __/ /
\____/ .___/\___/_/ /_/_/ |_/_/ /___/ /____/\___/_/ /_/\___/_/ /_/ /_/\__,_/ /_____/\__,_/_/_/\__,_/\___/_/
/_/
with <3 by @dr34mhacks
"""
def print_ascii_art():
print(colored(ASCII_ART, 'cyan'))
def display_usage():
print(colored("Usage: python3 openapi-schema-builder.py -i <input_file> -o <output_file> [--baseurl <base_url>] [--placeholders <placeholders>]", 'yellow'))
print("\nArguments:")
print(" -i, --input Input JSON file (Postman/Swagger)")
print(" -o, --output Output OpenAPI JSON file")
print(" --baseurl Base URL to replace placeholders in Postman collections")
print(" --placeholders Custom placeholders in the format if any (Optional): '{\"id\": \"123\", \"name\": \"Sid\"}'\n")
def detect_schema_type(json_data):
if "swagger" in json_data:
return "swagger"
elif "openapi" in json_data:
return "openapi"
elif "info" in json_data and "item" in json_data:
return "postman"
else:
return None
def extract_base_url(postman_schema, user_base_url=None):
if user_base_url:
return user_base_url
base_url = "http://example.com/api" # Default if no base URL can be determined
def find_base_url(items):
for item in items:
if 'item' in item:
found_url = find_base_url(item['item'])
if found_url:
return found_url
elif 'request' in item and 'url' in item['request']:
url_data = item['request']['url']
if isinstance(url_data, dict) and 'raw' in url_data:
return url_data['raw']
elif isinstance(url_data, str):
return url_data
return None
raw_url = find_base_url(postman_schema['item'])
if raw_url:
parsed_url = urlparse(raw_url)
if parsed_url.scheme and parsed_url.netloc:
base_url = f"{parsed_url.scheme}://{parsed_url.netloc}"
return base_url
def replace_placeholders(url, base_url):
url = url.replace('{{base_url}}', base_url)
url = url.replace('{{host}}', base_url)
return url
def replace_all_placeholders(json_data, base_url):
def recursive_replace(obj):
if isinstance(obj, dict):
for key, value in obj.items():
if key == 'url' and isinstance(value, str):
obj[key] = replace_placeholders(value, base_url)
else:
recursive_replace(value)
elif isinstance(obj, list):
for i in range(len(obj)):
recursive_replace(obj[i])
recursive_replace(json_data)
return json_data
def handle_placeholders(value):
if value == "<uuid>":
return {"type": "string", "format": "uuid"}
elif value == "<string>":
return {"type": "string"}
elif value == "<double>":
return {"type": "number", "format": "double"}
elif isinstance(value, str) and value.startswith("<") and value.endswith(">"):
return {"type": "string", "description": f"Placeholder: {value}"}
return {"type": "string"}
def convert_to_schema(data):
if isinstance(data, dict):
properties = {}
for key, value in data.items():
if isinstance(value, dict):
properties[key] = convert_to_schema(value)
elif isinstance(value, list):
if value:
item_schema = convert_to_schema(value[0])
properties[key] = {"type": "array", "items": item_schema}
else:
properties[key] = {"type": "array", "items": {"type": "object"}}
else:
properties[key] = handle_placeholders(value)
return {"type": "object", "properties": properties}
elif isinstance(data, list):
if data:
item_schema = convert_to_schema(data[0])
return {"type": "array", "items": item_schema}
else:
return {"type": "array", "items": {"type": "object"}}
return {"type": "string"}
def convert_postman_to_openapi(postman_schema, user_base_url=None, placeholder_map=None):
base_url = extract_base_url(postman_schema, user_base_url)
if user_base_url:
postman_schema = replace_all_placeholders(postman_schema, user_base_url)
openapi_schema = {
"openapi": "3.0.0",
"info": {
"title": postman_schema["info"]["name"],
"version": "1.0.0"
},
"servers": [
{"url": base_url}
],
"paths": {}
}
processed_count = 0
skipped_count = 0
skipped_endpoints = []
def process_item(item):
nonlocal processed_count, skipped_count
if 'request' in item and 'url' in item['request']:
try:
url_data = item['request']['url']
if isinstance(url_data, dict):
path = "/" + "/".join(url_data.get('path', []))
elif isinstance(url_data, str):
path = urlparse(url_data).path # Extract path from URL
else:
print(colored(f"Skipping item with unexpected URL format: {item['request']['url']}", 'yellow'))
skipped_count += 1
skipped_endpoints.append(item['name'])
return
path = replace_placeholders(path, base_url)
# Replace placeholders with values from the map
if placeholder_map:
for placeholder, value in placeholder_map.items():
path = path.replace(placeholder, value)
method = item["request"]["method"].lower()
if not path.startswith('/'):
print(colored(f"Skipping item with invalid path: {path}", 'yellow'))
skipped_count += 1
skipped_endpoints.append(item['name'])
return
openapi_schema["paths"].setdefault(path, {})
request_body = None
if method in ["post", "put", "patch"] and 'body' in item['request']:
body = item['request']['body']
if 'raw' in body and isinstance(body['raw'], str):
try:
raw_body = json.loads(body['raw'])
request_body = convert_to_schema(raw_body)
except json.JSONDecodeError:
print(colored(f"Warning: Could not decode body for {item['name']}", 'yellow'))
openapi_schema["paths"][path][method] = {
"summary": item["name"],
"responses": {
"200": {
"description": "Successful operation"
}
}
}
if request_body:
openapi_schema["paths"][path][method]["requestBody"] = {
"content": {
"application/json": {
"schema": request_body
}
}
}
print(colored(f"Processed item with URL: {path}", 'green'))
processed_count += 1
except Exception as e:
print(colored(f"Error processing item: {str(e)}", 'red'))
skipped_count += 1
skipped_endpoints.append(item['name'])
def process_items(items):
for item in items:
if 'item' in item:
process_items(item['item'])
else:
process_item(item)
process_items(postman_schema["item"])
# Log skipped endpoints if any
if skipped_endpoints:
with open('skipped_endpoints.json', 'w') as skipped_file:
json.dump(skipped_endpoints, skipped_file, indent=2)
print(colored(f"\n{skipped_count} endpoints were skipped. Details saved in 'skipped_endpoints.json'.", 'yellow'))
print(colored(f"\nTotal endpoints processed: {processed_count}", 'green'))
print(colored(f"Total endpoints skipped: {skipped_count}", 'yellow'))
return openapi_schema
if __name__ == "__main__":
print_ascii_art() # Always display ASCII art at the start, as why not?
if len(sys.argv) == 1:
display_usage()
sys.exit(0)
parser = argparse.ArgumentParser(description="Convert Postman or Swagger collections to OpenAPI schema.")
parser.add_argument("-i", "--input", required=True, help="Input JSON file (Postman/Swagger)")
parser.add_argument("-o", "--output", required=True, help="Output OpenAPI JSON file")
parser.add_argument("--baseurl", help="Base URL to replace placeholders in Postman collections")
parser.add_argument("--placeholders", help="Custom placeholders to replace in format: :placeholder=value, e.g., '{\"id\": \"123\", \"name\": \"Sid\"}'\n")
args = parser.parse_args()
input_file = args.input
output_file = args.output
base_url = args.baseurl
placeholder_map = {}
if args.placeholders:
placeholder_pairs = args.placeholders.split(',')
for pair in placeholder_pairs:
placeholder, value = pair.split('=')
placeholder_map[placeholder] = value
try:
with open(input_file, 'r') as infile:
json_data = json.load(infile)
schema_type = detect_schema_type(json_data)
if schema_type == "postman":
openapi_schema = convert_postman_to_openapi(json_data, base_url, placeholder_map)
with open(output_file, 'w') as outfile:
json.dump(openapi_schema, outfile, indent=2)
print(colored(f"OpenAPI schema has been successfully saved to {output_file}.", 'green'))
else:
print(colored("Unsupported schema type. Only Postman collections are supported.", 'red'))
except Exception as e:
print(colored(f"Error: {str(e)}", 'red'))