forked from Roger13/SwagDefGen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swaggerGen.js
197 lines (177 loc) · 6.07 KB
/
swaggerGen.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
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
function convert() {
'use strict';
// ---- Global variables ----
var inJSON, outSwagger, tabCount, indentator;
// ---- Functions definitions ----
function changeIndentation (count) {
/*
Assign 'indentator' a string beginning with newline and followed by 'count' tabs
Updates variable 'tabCount' with the number of tabs used
Global variables updated:
-identator
-tabcount
*/
let i;
if (count >= tabCount) {
i = tabCount
} else {
i = 0;
indentator = '\n';
}
for ( ; i < count; i++) {
indentator += '\t';
}
//Update tabCount
tabCount = count;
};
function conversorSelection (obj) {
/*
Selects which conversion method to call based on given obj
Global variables updated:
-outSwagger
*/
changeIndentation(tabCount+1);
if (typeof obj === "number") { //attribute is a number
convertNumber(obj);
} else if (Object.prototype.toString.call(obj) === '[object Array]') { //attribute is an array
convertArray(obj[0]);
} else if (typeof obj === "object") { //attribute is an object
convertObject(obj);
} else if (typeof obj === "string") { //attribute is a string
convertString(obj);
} else if (typeof obj === "boolean") { // attribute is a boolean
outSwagger += indentator + '"type": "boolean"';
} else { // not a valid Swagger type
alert ('Property type "'+typeof obj+'" not valid for Swagger definitions');
}
changeIndentation(tabCount-1);
};
function convertNumber (num) {
/*
Append to 'outSwagger' string with Swagger schema attributes relative to given number
Global variables updated:
-outSwagger
*/
if (num % 1 === 0 && !document.getElementById("noInt").checked) {
outSwagger += indentator + '"type": "integer",';
if (num < 2147483647 && num > -2147483647) {
outSwagger += indentator + '"format": "int32"';
} else if (Number.isSafeInteger(num)) {
outSwagger += indentator + '"format": "int64"';
} else {
outSwagger += indentator + '"format": "unsafe"';
}
} else {
outSwagger += indentator + '"type": "number"';
}
if (document.getElementById("requestExamples").checked) { //Log example if checkbox is checked
outSwagger += "," + indentator + '"example": "' + num + '"';
}
};
//date is ISO8601 format - https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14
function convertString (str) {
/*
Append to 'outSwagger' string with Swagger schema attributes relative to given string
Global variables updated:
-outSwagger
*/
let regxDate = /^(19|20)\d{2}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])$/,
regxDateTime = /^(19|20)\d{2}-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]).([0-1][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9](\.[0-9]{1,2})?(Z|(\+|\-)([0-1][0-9]|2[0-3]):[0-5][0-9])$/;
outSwagger += indentator + '"type": "string"';
if (regxDateTime.test(str)) {
outSwagger += ','
outSwagger += indentator + '"format": "date-time"';
} else if (regxDate.test(str)) {
outSwagger += ','
outSwagger += indentator + '"format": "date"';
}
if (document.getElementById("requestExamples").checked) { //Log example if checkbox is checked
outSwagger += "," + indentator + '"example": "' + str + '"';
}
};
function convertArray (obj) {
/*
Append to 'outSwagger' string with Swagger schema attributes relative to given array
Global variables updated:
-outSwagger
*/
outSwagger += indentator + '"type": "array",';
// ---- Begin items scope ----
outSwagger += indentator + '"items": {';
conversorSelection(obj);
outSwagger += indentator + '}';
// ---- End items scope ----
};
function convertObject (obj) {
/*
Append to 'outSwagger' string with Swagger schema attributes relative to given object
Global variables updated:
-outSwagger
*/
//Convert null attributes to given type
if (obj === null) {
outSwagger += indentator + '"type": "' + document.getElementById("nullType").value + '",';
outSwagger += indentator + '"format": "nullable"';
return;
}
// ---- Begin properties scope ----
outSwagger += indentator + '"type": "object",'
outSwagger += indentator + '"properties": {';
changeIndentation(tabCount + 1);
//For each attribute inside that object
for (var prop in obj) {
// ---- Begin property type scope ----
outSwagger += indentator + '"' + prop + '": {';
conversorSelection(obj[prop]);
outSwagger += indentator + '},'
// ---- End property type scope ----
}
changeIndentation(tabCount - 1);
if (Object.keys(obj).length > 0) { //At least 1 property inserted
outSwagger = outSwagger.substring(0, outSwagger.length - 1); //Remove last comma
outSwagger += indentator + '}'
} else { // No property inserted
outSwagger += ' }';
}
};
function format(value, yaml) {
/*
Convert JSON to YAML if yaml checkbox is checked
Global variables updated:
NONE
*/
if (yaml) {
return value.replace(/[{},"]+/g, '').replace(/\t/g, ' ').replace(/(^ *\n)/gm, '');
} else {
return value
}
}
// ---- Execution begins here ----
inJSON = document.getElementById("JSON").value;
try {
inJSON = JSON.parse(inJSON);
} catch (e) {
alert("Your JSON is invalid!\n(" + e +")");
return;
}
//For recursive functions to keep track of the tab spacing
tabCount = 0;
indentator = "\n";
// ---- Begin definitions ----
outSwagger = '"definitions": {';
changeIndentation(1);
//For each object inside the JSON
for (var obj in inJSON) {
// ---- Begin schema scope ----
outSwagger += indentator + '"' + obj + '": {'
conversorSelection(inJSON[obj]);
outSwagger += indentator + '},';
// ---- End schema scope ----
}
//Remove last comma
outSwagger = outSwagger.substring(0, outSwagger.length - 1);
// ---- End definitions ----
changeIndentation(tabCount-1);
outSwagger += indentator + '}';
document.getElementById("Swagger").value = format(outSwagger, document.getElementById("yamlOut").checked);
}