Socket
Socket
Sign inDemoInstall

ibm-openapi-support

Package Overview
Dependencies
Maintainers
2
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ibm-openapi-support - npm Package Compare versions

Comparing version 0.0.9 to 0.0.10

168

index.js

@@ -18,3 +18,3 @@ /*

let debug = require('debug')('ibm-openapi-support:index')
let utils = require('./utils')
let genutil = require('./utils')
let swaggerParser = require('swagger-parser')

@@ -24,2 +24,4 @@ let builderUtils = require('swaggerize-routes/lib/utils')

exports.loadAsync = genutil.loadAsync
function ensureValidAsync (loadedSwagger) {

@@ -34,2 +36,143 @@ debug('in ensureValidAsync')

function getRelatedModels (api) {
debug('in getRelatedModels')
let modelRelations = []
let newModel
Object.keys(api.definitions).forEach(function (modelName) {
debug('modelName: ', modelName)
let properties = api.definitions[modelName].properties
if (properties) {
Object.keys(properties).forEach(function (prop) {
let model = {
modelName: undefined,
plural: undefined,
pluralForm: undefined
}
if (properties[prop].$ref) {
newModel = genutil.getRefName(properties[prop].$ref)
model.modelName = modelName
model.plural = false
} else if (properties[prop].items && properties[prop].items.$ref) {
newModel = genutil.getRefName(properties[prop].items.$ref)
model.modelName = modelName
model.plural = true
model.pluralForm = prop
}
if (model.modelName) {
if (modelRelations[newModel]) {
modelRelations[newModel].push(model)
} else {
modelRelations[newModel] = [model]
}
}
})
}
})
return modelRelations
}
function extractModelsFromResponses (operation) {
// build a list of the models from data found in the responses sections of the swagger.
debug('in extractModelsFromResponses')
let models = []
if (operation.responses) {
Object.keys(operation.responses).forEach(responseCode => {
debug('response:', responseCode)
let response = operation.responses[responseCode]
if (response.schema) {
if (response.schema.$ref) {
models.push(genutil.getRefName(response.schema.$ref))
} else if (response.schema.items && response.schema.items.$ref) {
models.push(genutil.getRefName(response.schema.items.$ref))
}
}
})
}
return models
}
function extractModelsFromParameters (operation) {
// build a list of the models from data found in the parameters sections of the swagger.
debug('in extractModelsFromParameters')
let models = []
if (operation.parameters) {
operation.parameters.forEach(param => {
debug('param:', param)
if (param.schema) {
if (param.schema.$ref) {
models.push(genutil.getRefName(param.schema.$ref))
} else if (param.schema.items && param.schema.items.$ref) {
models.push(genutil.getRefName(param.schema.items.$ref))
}
}
})
}
return models
}
function getModelsFromPaths (api) {
// build a list of the models within in the swagger document that we need to construct.
debug('in getModelsFromPaths')
let models = []
Object.keys(api.paths).forEach(function (path) {
Object.keys(api.paths[path]).forEach(function (verb) {
if (!genutil.arrayContains(verb, builderUtils.verbs)) {
return
}
debug('verb:', path, verb)
let operation = api.paths[path][verb]
if (!operation) {
return
}
let pModels = extractModelsFromParameters(operation)
let rModels = extractModelsFromResponses(operation)
// now add the models from the parameters if they are not already in the models list
pModels.forEach(model => {
if (!models[model]) {
models[model] = api.definitions[model]
}
})
// now add the models from the responses if they are not already in the models list
rModels.forEach(model => {
if (!models[model]) {
models[model] = api.definitions[model]
}
})
})
})
return models
}
function getModels (api) {
// build a list of the models within in the swagger document that we need to construct.
debug('in getModels')
let models = getModelsFromPaths(api)
debug('models from paths:', models)
// add all returned models' children into models list
let relatedModels = getRelatedModels(api)
debug('related models:', relatedModels)
Object.keys(relatedModels).forEach(model => {
if (!models[model]) {
models[model] = api.definitions[model]
}
})
Object.keys(models).forEach(model => {
if (models[model] && models[model].properties) {
models[model] = api.definitions[model]
}
})
return models
}
function parseSwagger (api, formatters) {

@@ -69,3 +212,3 @@ debug('in parseSwagger')

// handle the schema ref
let ref = utils.getRefName(parameter.schema.$ref)
let ref = genutil.getRefName(parameter.schema.$ref)
refs[ref] = api.definitions[ref]

@@ -75,3 +218,3 @@ } else if (parameter.schema.items) {

if (parameter.schema.items.$ref) {
let ref = utils.getRefName(parameter.schema.items.$ref)
let ref = genutil.getRefName(parameter.schema.items.$ref)
// handle the schema ref

@@ -93,7 +236,7 @@ refs[ref] = api.definitions[ref]

// handle the schema ref
ref = utils.getRefName(responses[responseType].schema.$ref)
ref = genutil.getRefName(responses[responseType].schema.$ref)
refs[ref] = api.definitions[ref]
} else if (responses[responseType].schema.type && responses[responseType].schema.type === 'array') {
if (responses[responseType].schema.items && responses[responseType].schema.items.$ref) {
ref = utils.getRefName(responses[responseType].schema.items.$ref)
ref = genutil.getRefName(responses[responseType].schema.items.$ref)
refs[ref] = api.definitions[ref]

@@ -104,3 +247,3 @@ if (responses[responseType].schema.items) {

// handle the schema ref
ref = utils.getRefName(responses[responseType].schema.items.$ref)
ref = genutil.getRefName(responses[responseType].schema.items.$ref)
refs[ref] = api.definitions[ref]

@@ -129,3 +272,3 @@ }

// this property contains a definition reference.
name = utils.getRefName(properties[property].$ref)
name = genutil.getRefName(properties[property].$ref)
if (!refs[name]) {

@@ -137,3 +280,3 @@ refs[name] = api.definitions[name]

// this property contains a definition reference.
name = utils.getRefName(properties[property].items.$ref)
name = genutil.getRefName(properties[property].items.$ref)
if (!refs[name]) {

@@ -149,3 +292,4 @@ refs[name] = api.definitions[name]

let parsed = {basepath: basePath, resources: resources, refs: refs}
let models = getModels(api)
let parsed = {basepath: basePath, resources: resources, refs: refs, models: models}
return parsed

@@ -156,4 +300,4 @@ }

debug('in parse')
let swaggerType= undefined
let loaded= undefined
let swaggerType = undefined
let loaded = undefined

@@ -166,3 +310,3 @@ try {

loaded = YAML.safeLoad(swaggerStr)
swaggerType='yaml'
swaggerType = 'yaml'
} catch(e) {

@@ -169,0 +313,0 @@ throw new Error('document not in expected json or yaml format')

2

package.json
{
"name": "ibm-openapi-support",
"version": "0.0.9",
"version": "0.0.10",
"description": "utility for loading and parsing swagger",

@@ -5,0 +5,0 @@ "author": {

@@ -31,3 +31,3 @@ # ibm-openapi-support

* __index.parse__: This method is used to parse the swagger conument and build a dictionary of structures that contain the routes, resources and basepath required for generating API code. The parse method will use the supplied formatters to modify the path and the resource. This method takes two parameters:
* __index.parse__: This method is used to parse the swagger document and build a dictionary of structures that contain the routes, resources and basepath required for generating API code. The parse method will use the supplied formatters to modify the path and the resource. This method takes two parameters:
- stringified OpenApi (swagger) document.

@@ -77,3 +77,4 @@ - formatters dictionary: This contains formatters for the path _**pathFormatter**_ and for the resource _**resourceFormatter**_ The formatters take a path parameter and return a string.

'routes': this.parsedSwagger.resources[resource],
'basepath': this.parsedSwagger.basepath
'basepath': this.parsedSwagger.basepath,
'models': this.parsedSwagger.models
}

@@ -80,0 +81,0 @@ this.fs.copyTpl(this.templatePath('fromswagger/routers/router.js'), this.destinationPath(`server/routers/${resource}.js`), context)

@@ -11,3 +11,3 @@ {

"get": {
"description": "Gets 'Person' objects.",
"description": "Gets `Person` objects.",
"produces": [

@@ -31,3 +31,3 @@ "application/json"

"get": {
"description": "Gets 'Dinosaur' objects.",
"description": "Gets `Dinosaur` objects.",
"produces": [

@@ -65,9 +65,72 @@ "application/json"

"required": [
"age"
"age",
"heightInt",
"dietDictionaryInt32"
],
"properties": {
"age": {
"$ref": "#/definitions/age"
"type": "string",
"description": "comments go here"
},
"ages": {
"heightInt": {
"type": "integer"
},
"heightInt8": {
"type": "integer",
"format": "int8"
},
"heightUInt8": {
"type": "integer",
"format": "uint8"
},
"heightInt16": {
"type": "integer",
"format": "int16"
},
"heightUInt16": {
"type": "integer",
"format": "uint16"
},
"heightInt32": {
"type": "integer",
"format": "int32"
},
"heightUInt32": {
"type": "integer",
"format": "uint32"
},
"heightInt64": {
"type": "integer",
"format": "int64"
},
"heightUInt64": {
"type": "integer",
"format": "uint64"
},
"weight": {
"type": "number"
},
"weightDouble": {
"type": "number",
"format": "double"
},
"weightFloat": {
"type": "number",
"format": "float"
},
"toesBool": {
"type": "boolean"
},
"newAgeRef": {
"$ref": "#/definitions/newage"
},
"dietDictionaryInt32": {
"type": "object",
"additionalProperties": {
"type": "integer",
"format": "int32"
}
},
"agesArray": {
"type": "array",
"items": {

@@ -74,0 +137,0 @@ "$ref": "#/definitions/newage"

@@ -35,2 +35,14 @@ /*

it('returns true when the arrayContains find value in an array', function () {
let search = 'data'
let array = ['data']
assert(utils.arrayContains(search, array) === true)
})
it('returns false when the arrayContains cannot value in an array', function () {
let search = 'data'
let array = ['blahh']
assert(utils.arrayContains(search, array) === false)
})
it('can get a reference name from a swagger $reg value', function () {

@@ -180,2 +192,12 @@ assert(utils.getRefName('/helper/ff/test') === 'test')

assert(parsedSwagger.refs['newage'])
assert(Object.keys(parsedSwagger.models).length === 3)
assert(parsedSwagger.models['age'])
assert(parsedSwagger.models['age']['required'][0] === 'age')
assert(parsedSwagger.models['dino'])
assert(parsedSwagger.models['dino']['required'][0] === 'age')
assert(parsedSwagger.models['dino']['required'][1] === 'heightInt')
assert(parsedSwagger.models['dino']['required'][2] === 'dietDictionaryInt32')
assert(parsedSwagger.models['newage'])
assert(parsedSwagger.models['newage']['required'][0] === 'age')
assert(parsedSwagger.models)
})

@@ -182,0 +204,0 @@ })

@@ -36,2 +36,7 @@ /*

exports.arrayContains = function (search, array) {
// return true/false if search is/not found in array.
return array.indexOf(search) > -1
}
exports.validateFilePathOrURL = function (thePath) {

@@ -38,0 +43,0 @@ if (!thePath) {

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc