express-jsdoc-swagger
Advanced tools
Comparing version 1.4.0 to 1.5.0
{ | ||
"name": "express-jsdoc-swagger", | ||
"version": "1.4.0", | ||
"version": "1.5.0", | ||
"description": "Swagger OpenAPI 3.x generator", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -14,2 +14,3 @@ const errorMessage = require('../utils/errorMessage'); | ||
}; | ||
schema = { | ||
@@ -16,0 +17,0 @@ ...schema, |
@@ -6,16 +6,62 @@ const errorMessage = require('./errorMessage'); | ||
const validType = type => VALID_TYPES.includes(type); | ||
/** | ||
* This method checks the first item of the data type list to validate if | ||
* it contains any of the keywords representing the different schema types | ||
* in Swagger: 'oneOf', 'anyOf' or 'allOf'. | ||
* | ||
* @param {object[]} elements - List of data types for the property | ||
* @returns {string|null} Name of the schema type (if any) | ||
*/ | ||
const getSchemaType = elements => { | ||
const schemaType = elements[0].name; | ||
if (!VALID_TYPES.includes(schemaType)) { | ||
errorMessage(`SchemaType ${schemaType} invalid, it should be one of these ${VALID_TYPES.join(', ')}`); | ||
return null; | ||
} | ||
return schemaType; | ||
}; | ||
/** | ||
* This method receives an array of data types passed down to the | ||
* 'property' annotation, for example: | ||
* | ||
* > '{oneOf|string|null}'. | ||
* | ||
* The aim of this method is to process this array and generate | ||
* the schema for the property, including a list of its types. | ||
* | ||
* @param {object[]} elements - List of data types for the property | ||
* @returns Swagger schema for the property | ||
*/ | ||
const combineSchema = elements => { | ||
let schema = {}; | ||
if (!elements || elements.length === 0) return schema; | ||
const schemaType = elements[0].name; | ||
const [, ...types] = elements; | ||
if (validType(schemaType)) { | ||
// Check if 'null' is part of the listed types and remove it from the array | ||
const nullIndex = elements.findIndex(el => el.type === 'NullLiteral'); | ||
if (nullIndex > 0) { | ||
elements.splice(nullIndex, 1); | ||
schema = { nullable: true }; | ||
} | ||
const schemaType = getSchemaType(elements); | ||
const types = !schemaType ? elements : elements.slice(1); | ||
// If there are multiple types in the list, wrap them into a schema type | ||
// ('oneOf' will be used by default if none is specified) | ||
if (types.length > 1 || schemaType === 'allOf') { | ||
schema = { | ||
[schemaType]: types.map(type => refSchema(type)), | ||
...schema, | ||
[schemaType || 'oneOf']: types.map(type => refSchema(type)), | ||
}; | ||
} else { | ||
errorMessage(`SchemaType ${schemaType} invalid, it should be one of these ${VALID_TYPES.join(', ')}`); | ||
// If there's only a type in the list, don't wrap it in 'oneOf' or 'anyOf' blocks | ||
schema = { | ||
...schema, | ||
...refSchema(types[0]), | ||
}; | ||
} | ||
return schema; | ||
@@ -22,0 +68,0 @@ }; |
@@ -34,3 +34,4 @@ const validateTypes = require('./validateTypes'); | ||
// support null | ||
if (nameValue.type === 'NullLiteral') return {}; | ||
if (nameValue.type === 'NullLiteral') return { nullable: true }; | ||
const isPrimitive = validateTypes(nameValue); | ||
@@ -37,0 +38,0 @@ return isPrimitive ? { type: nameValue } : { $ref: `${REF_ROUTE}${nameValue}` }; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65316
1265