Last active
January 19, 2018 20:28
-
-
Save mspclaims/4c2ff3d624b278b4a02a8cc72bd3f6cd to your computer and use it in GitHub Desktop.
Generate Swagger model definition from JSON schema files (http://json-schema.org/draft-06/schema)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let getSwaggerDefinitions = function(){ | |
let schemaPassword = require('../schemas/changePasswordRequest.json'); | |
let schemaUser = require('../schemas/createUserRequest.json'); | |
let schemaLogin = require('../schemas/loginUserRequest.json'); | |
let schemaProfile = require('../schemas/profilePutSchema.json'); | |
let schemas={password:schemaPassword, user:schemaUser, login: schemaLogin, profile: schemaProfile}; | |
//let schemas={profile: schemaProfile}; | |
let reply={}; | |
for (var key in schemas) { | |
let upperLevel = schemas[key].properties; | |
let hasNested = false; | |
_.forEach(upperLevel, function(item, itemKey){ | |
hasNested = false; | |
let processed = false; | |
if (item.oneOf != undefined) hasNested = true; | |
if (item.properties != undefined) hasNested = true; | |
if (item.properties && itemKey !='access' ) { | |
let model={}; | |
model.type="object"; | |
model.properties = item.properties; | |
if (reply[key]==undefined) { | |
reply[key]={}; | |
reply[key].type="object"; | |
reply[key].properties={}; | |
} | |
reply[key].properties[itemKey]=model; | |
processed= true; | |
} | |
if (item.oneOf && itemKey !='access' && reply[itemKey]==undefined) { | |
if (item.oneOf.length!=undefined) { | |
for (choice in item.oneOf) { | |
let model={}; | |
model.type="object"; | |
model.properties = item.oneOf[choice].properties; | |
let uniqKey=itemKey + choice.toString(); | |
reply[uniqKey]={}; | |
reply[uniqKey].type="object"; | |
reply[uniqKey].properties={}; | |
reply[uniqKey].properties[key]=model; | |
} | |
} | |
processed = true; | |
} | |
if (!processed && !hasNested && itemKey !='access' && item.type == 'object') { | |
let model={}; | |
model.type="object"; | |
model.properties = item.properties; | |
if (reply[key]==undefined) { | |
reply[key]={}; | |
reply[key].type="object"; | |
reply[key].properties={}; | |
} | |
reply[key].properties[itemKey]=model; | |
} | |
}); | |
if (hasNested==false) { | |
if (key !='access' && reply[key]==undefined) { | |
let model={}; | |
model.type="object"; | |
model.properties = upperLevel; | |
reply[key]=model; | |
} | |
} | |
} | |
return reply; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment