-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswagger.js
57 lines (46 loc) · 1.6 KB
/
swagger.js
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
const fs = require('fs');
const https = require('https');
async function main(){
await new Promise(resolve => {
const file = fs.createWriteStream("swagger-raw.json");
https.get("https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json", function(response) {
response.pipe(file);
});
file.on('finish', () => {
resolve();
})
})
const json = JSON.parse(fs.readFileSync('swagger-raw.json'));
const rawDefinitions = json.definitions;
function linkedPropertySearch(key, acc){
console.log(key);
key = key.replace('#/definitions/', '');
if(acc.includes(key)){
return;
}
acc.push(key);
const props = rawDefinitions[key].properties;
if(!props){
return;
}
const propsKeys = Object.keys(props);
propsKeys.forEach(propKey => {
if(props[propKey]['$ref']){
linkedPropertySearch(props[propKey]['$ref'], acc);
return;
}
if(props[propKey]['items'] && props[propKey]['items']['$ref']){
linkedPropertySearch(props[propKey]['items']['$ref'], acc)
}
});
}
const linkedProperties = [];
linkedPropertySearch('io.k8s.api.networking.v1.NetworkPolicy', linkedProperties);
const definitions = {};
linkedProperties.forEach((key) => {
definitions[key] = rawDefinitions[key];
});
const result = {definitions};
fs.writeFileSync('swagger.json', JSON.stringify(result, null, "\t"));
}
main();