-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathswagger-subset.html
361 lines (310 loc) · 9.24 KB
/
swagger-subset.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Swagger Subset</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: Helvetica, Arial, sans-serif;
margin: 0;
padding: 20px;
line-height: 1.6;
}
h1, h2 {
margin-top: 0;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
textarea {
width: 100%;
min-height: 200px;
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
font-family: monospace;
}
button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 15px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 4px 2px;
cursor: pointer;
border-radius: 4px;
}
button:hover {
background-color: #45a049;
}
.error {
color: red;
margin-bottom: 15px;
}
#pathList {
margin: 20px 0;
max-height: 400px;
overflow-y: auto;
border: 1px solid #ddd;
padding: 10px;
border-radius: 4px;
}
.path-item {
margin-bottom: 8px;
padding-bottom: 8px;
border-bottom: 1px solid #eee;
}
.method {
margin-left: 20px;
display: block;
padding: 4px 0;
}
.method-get {
color: #61affe;
}
.method-post {
color: #49cc90;
}
.method-put {
color: #fca130;
}
.method-delete {
color: #f93e3e;
}
.controls {
margin: 10px 0;
}
.path-tag {
display: inline-block;
background-color: #eee;
padding: 2px 6px;
border-radius: 4px;
margin-left: 8px;
font-size: 12px;
}
#outputContainer {
display: none;
margin-top: 20px;
}
.copy-btn {
background-color: #2196F3;
}
.copy-btn:hover {
background-color: #0b7dda;
}
</style>
</head>
<body>
<div class="container">
<h1>Swagger Subset</h1>
<div>
<h2>Paste your Swagger JSON</h2>
<textarea id="swaggerInput" placeholder="Paste your Swagger JSON here..."></textarea>
<button id="parseBtn">Parse Swagger</button>
<div id="error" class="error"></div>
</div>
<div id="pathsContainer" style="display: none;">
<h2>Select paths to include</h2>
<div class="controls">
<button id="selectAllBtn">Select All</button>
<button id="deselectAllBtn">Deselect All</button>
</div>
<div id="pathList"></div>
<button id="generateBtn">Generate JSON</button>
</div>
<div id="outputContainer">
<h2>Output JSON</h2>
<div class="controls">
<button id="copyBtn" class="copy-btn">Copy to Clipboard</button>
</div>
<textarea id="outputJson" readonly></textarea>
</div>
</div>
<script type="module">
// Parse Swagger JSON and display paths as checkboxes
document.getElementById('parseBtn').addEventListener('click', parseSwagger);
document.getElementById('selectAllBtn').addEventListener('click', selectAll);
document.getElementById('deselectAllBtn').addEventListener('click', deselectAll);
document.getElementById('generateBtn').addEventListener('click', generateJson);
document.getElementById('copyBtn').addEventListener('click', copyToClipboard);
let swaggerData = null;
function parseSwagger() {
const input = document.getElementById('swaggerInput').value;
const errorElem = document.getElementById('error');
errorElem.textContent = '';
try {
swaggerData = JSON.parse(input);
if (!swaggerData.paths) {
throw new Error("No 'paths' property found in the Swagger JSON");
}
displayPaths(swaggerData.paths);
document.getElementById('pathsContainer').style.display = 'block';
document.getElementById('outputContainer').style.display = 'none';
} catch (err) {
errorElem.textContent = `Error parsing JSON: ${err.message}`;
}
}
function displayPaths(paths) {
const pathListElem = document.getElementById('pathList');
pathListElem.innerHTML = '';
// Group paths by tag
const pathsByTag = {};
Object.keys(paths).sort().forEach(path => {
const pathItem = document.createElement('div');
pathItem.className = 'path-item';
const pathLabel = document.createElement('label');
pathLabel.innerHTML = `<strong>${path}</strong>`;
pathItem.appendChild(pathLabel);
Object.keys(paths[path]).forEach(method => {
const endpoint = paths[path][method];
const methodId = `${path}-${method}`;
const methodElem = document.createElement('label');
methodElem.className = `method method-${method}`;
// Create checkbox
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = methodId;
checkbox.dataset.path = path;
checkbox.dataset.method = method;
// Create method label
methodElem.appendChild(checkbox);
methodElem.appendChild(document.createTextNode(` ${method.toUpperCase()}`));
// Add tags if available
if (endpoint.tags && endpoint.tags.length > 0) {
const tagSpan = document.createElement('span');
tagSpan.className = 'path-tag';
tagSpan.textContent = endpoint.tags[0];
methodElem.appendChild(tagSpan);
// Group by first tag
const tag = endpoint.tags[0];
if (!pathsByTag[tag]) {
pathsByTag[tag] = [];
}
pathsByTag[tag].push(methodElem);
}
pathItem.appendChild(methodElem);
});
pathListElem.appendChild(pathItem);
});
}
function selectAll() {
const checkboxes = document.querySelectorAll('#pathList input[type="checkbox"]');
checkboxes.forEach(checkbox => {
checkbox.checked = true;
});
}
function deselectAll() {
const checkboxes = document.querySelectorAll('#pathList input[type="checkbox"]');
checkboxes.forEach(checkbox => {
checkbox.checked = false;
});
}
function getSelectedEndpoints() {
const selected = [];
const checkboxes = document.querySelectorAll('#pathList input[type="checkbox"]:checked');
checkboxes.forEach(checkbox => {
selected.push({
path: checkbox.dataset.path,
method: checkbox.dataset.method
});
});
return selected;
}
function findReferencedDefinitions(obj, referencedDefs = new Set()) {
if (!obj) return referencedDefs;
if (typeof obj === 'object') {
if (Array.isArray(obj)) {
obj.forEach(item => findReferencedDefinitions(item, referencedDefs));
} else {
// Check for $ref property
if (obj['$ref'] && typeof obj['$ref'] === 'string') {
const refParts = obj['$ref'].split('/');
if (refParts[1] === 'definitions' && refParts.length > 2) {
referencedDefs.add(refParts[2]);
}
}
// Recursively check all properties
Object.values(obj).forEach(value => {
findReferencedDefinitions(value, referencedDefs);
});
}
}
return referencedDefs;
}
function addDependentDefinitions(definitions, defName, allDefs, processedDefs = new Set()) {
if (processedDefs.has(defName)) return;
processedDefs.add(defName);
const def = allDefs[defName];
if (!def) return;
// Find references in this definition
const referencedDefs = findReferencedDefinitions(def);
// Add this definition to our result
definitions[defName] = def;
// Recursively process any referenced definitions
referencedDefs.forEach(refDef => {
addDependentDefinitions(definitions, refDef, allDefs, processedDefs);
});
}
function generateJson() {
const selectedEndpoints = getSelectedEndpoints();
if (selectedEndpoints.length === 0) {
document.getElementById('error').textContent = 'Please select at least one endpoint';
return;
}
// Create a new paths object with only selected endpoints
const filteredPaths = {};
const referencedDefs = new Set();
selectedEndpoints.forEach(endpoint => {
const { path, method } = endpoint;
if (!filteredPaths[path]) {
filteredPaths[path] = {};
}
filteredPaths[path][method] = swaggerData.paths[path][method];
// Find all referenced definitions in this endpoint
findReferencedDefinitions(swaggerData.paths[path][method]).forEach(def => {
referencedDefs.add(def);
});
});
// Create a filtered definitions object
const filteredDefinitions = {};
if (swaggerData.definitions) {
referencedDefs.forEach(defName => {
addDependentDefinitions(filteredDefinitions, defName, swaggerData.definitions);
});
}
// Create the output JSON with only paths and definitions
const output = {
paths: filteredPaths
};
// Only include definitions if there are any
if (Object.keys(filteredDefinitions).length > 0) {
output.definitions = filteredDefinitions;
}
// Display the output
document.getElementById('outputJson').value = JSON.stringify(output, null, 2);
document.getElementById('outputContainer').style.display = 'block';
}
function copyToClipboard() {
const outputElem = document.getElementById('outputJson');
outputElem.select();
document.execCommand('copy');
const copyBtn = document.getElementById('copyBtn');
const originalText = copyBtn.textContent;
copyBtn.textContent = 'Copied!';
setTimeout(() => {
copyBtn.textContent = originalText;
}, 2000);
}
</script>
</body>
</html>