-
Notifications
You must be signed in to change notification settings - Fork 793
/
fix-swagger-type
executable file
·59 lines (52 loc) · 1.46 KB
/
fix-swagger-type
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
#!/usr/bin/env node
const fs = require("fs");
// Function to recursively fix the types
function fixTypes(obj) {
if (typeof obj === "object" && obj !== null) {
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
if (
typeof obj[k] === "object" &&
obj[k].hasOwnProperty("format") &&
(obj[k].format === "int64" || obj[k].format === "int32") &&
obj[k].type === "number"
) {
obj[k].type = "integer";
}
fixTypes(obj[k]);
}
}
}
}
if (require.main === module) {
// Check if a file argument was provided
if (process.argv[2]) {
// Load the JSON file
let data = JSON.parse(fs.readFileSync(process.argv[2], "utf8"));
// Fix the types
fixTypes(data);
// Write the fixed JSON back to the file
fs.writeFileSync(process.argv[2], JSON.stringify(data, null, 2), "utf8");
} else if (!process.stdin.isTTY) {
// Read from stdin
let json = "";
process.stdin.on("readable", () => {
let chunk;
while ((chunk = process.stdin.read()) !== null) {
json += chunk;
}
});
process.stdin.on("end", () => {
let data = JSON.parse(json);
// Fix the types
fixTypes(data);
// Write the fixed JSON to stdout
process.stdout.write(JSON.stringify(data, null, 2));
});
} else {
console.error(
"No input provided. Please provide a file argument or pipe in some data."
);
process.exit(1);
}
}