Adding version as argument of getOptions, resolving getOptions arguments when the user does not provide all

This commit is contained in:
Erik Mendoza
2022-01-17 22:39:10 -06:00
parent 1e344abd20
commit bdfebb9451

View File

@@ -1,9 +1,35 @@
const _ = require('lodash');
const _ = require('lodash'),
VALID_MODES = ['document', 'use'],
VERSION30 = '3.0',
VERSION31 = '3.1',
VALID_VERSIONS = [VERSION30, VERSION31];
/**
* Takes a list of arguments and resolve them acording its content
* @param {array} args The arguments that will be resolved
* @returns {array} The list of arguments after have been resolved
*/
function handleArguments(args) {
let mode = 'document',
criteria = {},
version = VERSION30;
args.forEach((argument) => {
if (typeof argument === 'object' && Object.keys(argument).length > 0) {
criteria = argument;
}
else if (VALID_MODES.includes(argument)) {
mode = argument;
}
else if (VALID_VERSIONS.includes(argument)) {
version = argument;
}
});
return { mode, criteria, version };
}
module.exports = {
// default options
// if mode=document, returns an array of name/id/default etc.
/**
* name - human-readable name for the option
* id - key to pass the option with
@@ -19,14 +45,15 @@ module.exports = {
* @param {Object} criteria Decribes required criteria for options to be returned. can have properties
* external: <boolean>
* usage: <array> (Array of supported usage type - CONVERSION, VALIDATION)
* @param {string} version The specification version provided
* @returns {mixed} An array or object (depending on mode) that describes available options
*/
getOptions: function(mode = 'document', criteria = {}) {
getOptions: function(mode = 'document', criteria = {}, version = '3.0') {
// Override mode & criteria if first arg is criteria (objects)
if (typeof mode === 'object') {
criteria = mode;
mode = 'document';
}
const resolvedArguments = handleArguments([mode, criteria, version]);
mode = resolvedArguments.mode;
criteria = resolvedArguments.criteria;
version = resolvedArguments.version;
let optsArray = [
{
@@ -39,7 +66,8 @@ module.exports = {
' If “Fallback” is selected, the request will be named after one of the following schema' +
' values: `description`, `operationid`, `url`.',
external: true,
usage: ['CONVERSION', 'VALIDATION']
usage: ['CONVERSION', 'VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Set indent character',
@@ -49,7 +77,8 @@ module.exports = {
availableOptions: ['Space', 'Tab'],
description: 'Option for setting indentation character',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Collapse redundant folders',
@@ -59,7 +88,8 @@ module.exports = {
description: 'Importing will collapse all folders that have only one child element and lack ' +
'persistent folder-level data.',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Optimize conversion',
@@ -69,7 +99,8 @@ module.exports = {
description: 'Optimizes conversion for large specification, disabling this option might affect' +
' the performance of conversion.',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Request parameter generation',
@@ -82,7 +113,8 @@ module.exports = {
' [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject)' +
' in the schema.',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Response parameter generation',
@@ -95,7 +127,8 @@ module.exports = {
' [example](https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#exampleObject)' +
' in the schema.',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Folder organization',
@@ -105,7 +138,8 @@ module.exports = {
availableOptions: ['Paths', 'Tags'],
description: 'Select whether to create folders according to the specs paths or tags.',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Enable Schema Faking',
@@ -114,7 +148,8 @@ module.exports = {
default: true,
description: 'Whether or not schemas should be faked.',
external: false,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Schema resolution nesting limit',
@@ -125,7 +160,8 @@ module.exports = {
' result in more time to convert collection depending on complexity of specification. (To make sure this' +
' option works correctly "optimizeConversion" option needs to be disabled)',
external: false,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Include auth info in example requests',
@@ -134,7 +170,8 @@ module.exports = {
default: true,
description: 'Select whether to include authentication parameters in the example request',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Short error messages during request <> schema validation',
@@ -143,7 +180,8 @@ module.exports = {
default: false,
description: 'Whether detailed error messages are required for request <> schema validation operations.',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Properties to ignore during validation',
@@ -154,7 +192,8 @@ module.exports = {
' Must be sent as an array of strings. Valid inputs in the array: PATHVARIABLE, QUERYPARAM,' +
' HEADER, BODY, RESPONSE_HEADER, RESPONSE_BODY',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Whether MISSING_IN_SCHEMA mismatches should be returned',
@@ -164,7 +203,8 @@ module.exports = {
description: 'MISSING_IN_SCHEMA indicates that an extra parameter was included in the request. For most ' +
'use cases, this need not be considered an error.',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Show detailed body validation messages',
@@ -174,7 +214,8 @@ module.exports = {
description: 'Determines whether to show detailed mismatch information for application/json content ' +
'in the request/response body.',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Suggest fixes if available',
@@ -183,7 +224,8 @@ module.exports = {
default: false,
description: 'Whether to provide fixes for patching corresponding mismatches.',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Show Metadata validation messages',
@@ -192,7 +234,8 @@ module.exports = {
default: false,
description: 'Whether to show mismatches for incorrect name and description of request',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Ignore mismatch for unresolved postman variables',
@@ -201,7 +244,8 @@ module.exports = {
default: false,
description: 'Whether to ignore mismatches resulting from unresolved variables in the Postman request',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Enable strict request matching',
@@ -211,7 +255,8 @@ module.exports = {
description: 'Whether requests should be strictly matched with schema operations. Setting to true will not ' +
'include any matches where the URL path segments don\'t match exactly.',
external: true,
usage: ['VALIDATION']
usage: ['VALIDATION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Disable optional parameters',
@@ -220,7 +265,8 @@ module.exports = {
default: false,
description: 'Whether to set optional parameters as disabled',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
},
{
name: 'Keep implicit headers',
@@ -229,7 +275,8 @@ module.exports = {
default: false,
description: 'Whether to keep implicit headers from the OpenAPI specification, which are removed by default.',
external: true,
usage: ['CONVERSION']
usage: ['CONVERSION'],
supportedIn: [VERSION30, VERSION31]
}
];
@@ -267,7 +314,8 @@ module.exports = {
// options to be used as documentation
return _.filter(optsArray, (opt) => {
// only return options that are not disabled
return opt.disabled !== true;
return opt.disabled !== true &&
opt.supportedIn.includes(version);
});
}
};