Added tabspacing support inside textarea, added primitive type support at root level, refactored code and added comments

This commit is contained in:
RogerBernardo
2017-03-08 11:28:37 -03:00
parent 081cc927a2
commit 365c14ec5d
2 changed files with 116 additions and 78 deletions

View File

@@ -4,6 +4,21 @@
<meta charset="UTF-8"/> <meta charset="UTF-8"/>
<title>Swagger Generator</title> <title>Swagger Generator</title>
<script src="swaggerGen.js" type="text/javascript"></script> <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> <style>
textarea{ textarea{
display: inline-block; display: inline-block;
@@ -15,7 +30,7 @@
} }
</style> </style>
</head> </head>
<body> <body onload="init();">
<h1>Swagger Definition Objects Generator</h1> <h1>Swagger Definition Objects Generator</h1>
<p>Add your JSON mock to generate Swagger definition objects.</p> <p>Add your JSON mock to generate Swagger definition objects.</p>
<textarea id="JSON" rows="20" cols="55" placeholder="Type your JSON"> <textarea id="JSON" rows="20" cols="55" placeholder="Type your JSON">

View File

@@ -1,20 +1,17 @@
function convert() { function convert() {
'use strict'; 'use strict';
var inJSON = document.getElementById("JSON").value; var inJSON, outSwagger, tabCount, indentator
try { ;
inJSON = JSON.parse(inJSON); // ---- Functions definitions ----
} 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": {';
function changeIndentation (count) { 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; let i;
if (count >= tabCount) { if (count >= tabCount) {
i = tabCount i = tabCount
@@ -29,8 +26,38 @@ function convert() {
tabCount = count; 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) { function convertNumber (num) {
if (num % 1 === 0) { /*
Append to 'outSwagger' string with Swagger schema attributes relative to given number
Global variables updated:
-outSwagger
*/
if (num % 1 === 0) {
outSwagger += indentator + '"type": "integer",'; outSwagger += indentator + '"type": "integer",';
if (num < 2147483647 && num > -2147483647) { if (num < 2147483647 && num > -2147483647) {
outSwagger += indentator + '"format": "int32"'; outSwagger += indentator + '"format": "int32"';
@@ -42,55 +69,63 @@ function convert() {
} else { } else {
outSwagger += indentator + '"type": "number"'; 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 //date is ISO8601 format - https://xml2rfc.tools.ietf.org/public/rfc/html/rfc3339.html#anchor14
function convertDate (str) { 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])$/, 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])$/; 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])$/;
if (regxDateTime.test(str)) {
outSwagger += indentator + '"type": "string"';
if (regxDateTime.test(str)) {
outSwagger += ',' outSwagger += ','
outSwagger += indentator + '"format": "date-time"'; outSwagger += indentator + '"format": "date-time"';
} else if (regxDate.test(str)) { } else if (regxDate.test(str)) {
outSwagger += ',' outSwagger += ','
outSwagger += indentator + '"format": "date"'; outSwagger += indentator + '"format": "date"';
}
if (document.getElementById("requestExamples").checked) { //Log example if checkbox is checked
outSwagger += "," + indentator + '"description": "Ex: ' + str + '"';
} }
}; };
function convertArray (obj) { function convertArray (obj) {
/*
Append to 'outSwagger' string with Swagger schema attributes relative to given array
Global variables updated:
-outSwagger
*/
outSwagger += indentator + '"type": "array",'; outSwagger += indentator + '"type": "array",';
outSwagger += indentator + '"items": {'; // ---- Begin items scope ----
changeIndentation(tabCount + 1); outSwagger += indentator + '"items": {';
conversorSelection(obj);
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);
outSwagger += indentator + '}'; outSwagger += indentator + '}';
// ---- End items scope ----
}; };
function convertObject (obj) { 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 //Convert null attributes to given type
if (obj === null) { if (obj === null) {
outSwagger += indentator + '"type": "' + document.getElementById("nullType").value + '",'; outSwagger += indentator + '"type": "' + document.getElementById("nullType").value + '",';
outSwagger += indentator + '"format": "nullable"'; outSwagger += indentator + '"format": "nullable"';
return; return;
} }
// ---- Begin properties scope ---- // ---- Begin properties scope ----
outSwagger += indentator + '"type": "object",' outSwagger += indentator + '"type": "object",'
@@ -100,29 +135,10 @@ function convert() {
for (var prop in obj) { for (var prop in obj) {
// ---- Begin property type scope ---- // ---- Begin property type scope ----
outSwagger += indentator + '"' + prop + '": {'; outSwagger += indentator + '"' + prop + '": {';
changeIndentation(tabCount + 1); conversorSelection(obj[prop]);
if (typeof obj[prop] === "number") { //attribute is a number outSwagger += indentator + '},'
convertNumber(obj[prop]); // ---- End property type scope ----
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);
outSwagger += indentator + '},'
}
changeIndentation(tabCount - 1); changeIndentation(tabCount - 1);
if (Object.keys(obj).length > 0) { //At least 1 property inserted if (Object.keys(obj).length > 0) { //At least 1 property inserted
@@ -132,28 +148,35 @@ function convert() {
outSwagger += ' }'; outSwagger += ' }';
} }
}; };
// ---- Execution begins here ----
inJSON = document.getElementById("JSON").value;
try {
inJSON = JSON.parse(inJSON);
} catch (e) {
alert("Your JSON is invalid!\n(" + e +")");
return;
}
//Execution begins here //For recursive functions to keep track of the tab spacing
tabCount = 0;
indentator = "\n";
// ---- Begin definitions ----
outSwagger = '"definitions": {';
changeIndentation(1); changeIndentation(1);
//For each object inside the JSON //For each object inside the JSON
for (var obj in inJSON) { for (var obj in inJSON) {
if (typeof inJSON[obj] === "object") { // ---- Begin schema scope ----
// ---- Begin object scope ----
outSwagger += indentator + '"' + obj + '": {' outSwagger += indentator + '"' + obj + '": {'
changeIndentation(tabCount+1); conversorSelection(inJSON[obj]);
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);
outSwagger += indentator + '},'; outSwagger += indentator + '},';
} // ---- End schema scope ----
} }
//Remove last comma //Remove last comma
outSwagger = outSwagger.substring(0, outSwagger.length - 1); outSwagger = outSwagger.substring(0, outSwagger.length - 1);
// ---- End definitions ----
changeIndentation(tabCount-1); changeIndentation(tabCount-1);
outSwagger += indentator + '}'; outSwagger += indentator + '}';
document.getElementById("Swagger").value = outSwagger; document.getElementById("Swagger").value = outSwagger;
} }