mirror of
https://github.com/LukeHagar/SwagDefGen.git
synced 2025-12-09 20:57:45 +00:00
Added tabspacing support inside textarea, added primitive type support at root level, refactored code and added comments
This commit is contained in:
17
index.html
17
index.html
@@ -4,6 +4,21 @@
|
||||
<meta charset="UTF-8"/>
|
||||
<title>Swagger Generator</title>
|
||||
<script src="swaggerGen.js" type="text/javascript"></script>
|
||||
<script>
|
||||
function init() {
|
||||
//Handle tabs inside JSON textarea
|
||||
let txtArea = document.getElementById('JSON');
|
||||
txtArea.onkeydown = function(e){
|
||||
if(e.keyCode==9 || e.which==9){
|
||||
e.preventDefault();
|
||||
let s = this.selectionStart;
|
||||
this.value = this.value.substring(0,this.selectionStart) + "\t" + this.value.substring(this.selectionEnd);
|
||||
this.selectionEnd = s+1;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
</script>
|
||||
<style>
|
||||
textarea{
|
||||
display: inline-block;
|
||||
@@ -15,7 +30,7 @@
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<body onload="init();">
|
||||
<h1>Swagger Definition Objects Generator</h1>
|
||||
<p>Add your JSON mock to generate Swagger definition objects.</p>
|
||||
<textarea id="JSON" rows="20" cols="55" placeholder="Type your JSON">
|
||||
|
||||
165
swaggerGen.js
165
swaggerGen.js
@@ -1,20 +1,17 @@
|
||||
function convert() {
|
||||
'use strict';
|
||||
var 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
|
||||
var tabCount = 0;
|
||||
var indentator = "\n";
|
||||
//Begin definitions
|
||||
var outSwagger = '"definitions": {';
|
||||
|
||||
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
|
||||
@@ -29,7 +26,37 @@ function convert() {
|
||||
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) {
|
||||
outSwagger += indentator + '"type": "integer",';
|
||||
if (num < 2147483647 && num > -2147483647) {
|
||||
@@ -42,50 +69,58 @@ function convert() {
|
||||
} else {
|
||||
outSwagger += indentator + '"type": "number"';
|
||||
}
|
||||
if (document.getElementById("requestExamples").checked) { //Log example if checkbox is checked
|
||||
outSwagger += "," + indentator + '"description": "Ex: ' + num + '"';
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
//ISO8601 format - https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14
|
||||
function convertDate (str) {
|
||||
//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 + '"description": "Ex: ' + 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": {';
|
||||
changeIndentation(tabCount + 1);
|
||||
|
||||
if (typeof obj === "number") { //attribute is a number
|
||||
convertNumber(obj);
|
||||
if (document.getElementById("requestExamples").checked) {
|
||||
outSwagger += "," + indentator + '"description": "Ex: ' + 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 {
|
||||
outSwagger += indentator + '"type": "' + typeof obj + '"';
|
||||
if (typeof obj === "string") {
|
||||
convertDate(obj);
|
||||
}
|
||||
if (document.getElementById("requestExamples").checked) {
|
||||
outSwagger += "," + indentator + '"description": "Ex: ' + obj + '"';
|
||||
}
|
||||
|
||||
}
|
||||
changeIndentation(tabCount - 1);
|
||||
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 + '",';
|
||||
@@ -100,28 +135,9 @@ function convert() {
|
||||
for (var prop in obj) {
|
||||
// ---- Begin property type scope ----
|
||||
outSwagger += indentator + '"' + prop + '": {';
|
||||
changeIndentation(tabCount + 1);
|
||||
if (typeof obj[prop] === "number") { //attribute is a number
|
||||
convertNumber(obj[prop]);
|
||||
if (document.getElementById("requestExamples").checked) {
|
||||
outSwagger += "," + indentator + '"description": "Ex: ' + obj[prop] + '"';
|
||||
}
|
||||
} else if (Object.prototype.toString.call(obj[prop]) === '[object Array]') { //attribute is an array
|
||||
convertArray(obj[prop][0], prop);
|
||||
} else if (typeof obj[prop] === "object") { //attribute is an object
|
||||
convertObject(obj[prop], prop);
|
||||
} else {
|
||||
outSwagger += indentator + '"type": "' + typeof obj[prop] + '"';
|
||||
if (typeof obj[prop] === "string") {
|
||||
convertDate(obj[prop]);
|
||||
}
|
||||
if (document.getElementById("requestExamples").checked) {
|
||||
outSwagger += "," + indentator + '"description": "Ex: ' + obj[prop] + '"';
|
||||
}
|
||||
}
|
||||
// ---- End property type scope ----
|
||||
changeIndentation(tabCount - 1);
|
||||
conversorSelection(obj[prop]);
|
||||
outSwagger += indentator + '},'
|
||||
// ---- End property type scope ----
|
||||
}
|
||||
|
||||
changeIndentation(tabCount - 1);
|
||||
@@ -133,27 +149,34 @@ function convert() {
|
||||
}
|
||||
};
|
||||
|
||||
//Execution begins here
|
||||
// ---- 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) {
|
||||
if (typeof inJSON[obj] === "object") {
|
||||
// ---- Begin object scope ----
|
||||
// ---- Begin schema scope ----
|
||||
outSwagger += indentator + '"' + obj + '": {'
|
||||
changeIndentation(tabCount+1);
|
||||
if (Object.prototype.toString.call(inJSON[obj]) === '[object Array]') {
|
||||
convertArray(inJSON[obj][0], obj);
|
||||
} else {
|
||||
convertObject(inJSON[obj], obj);
|
||||
}
|
||||
// ---- End object scope ----
|
||||
changeIndentation(tabCount-1);
|
||||
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 = outSwagger;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user