From 1ae90b9f6a550ec171795a8cb7d87dab4f1f684d Mon Sep 17 00:00:00 2001 From: Zomato Date: Mon, 16 Mar 2020 21:02:41 +0530 Subject: [PATCH 1/2] added jsoc for functions --- index.js | 25 ++++++++++ lib/error.js | 1 + lib/parse.js | 8 ++++ lib/schemaUtils.js | 107 ++++++++++++++++++++++++++++++++++++++---- lib/schemapack.js | 21 +++++++++ lib/utils.js | 7 ++- lib/xmlSchemaFaker.js | 8 ++++ 7 files changed, 168 insertions(+), 9 deletions(-) diff --git a/index.js b/index.js index c8a8bce72..87454f053 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,19 @@ const SchemaPack = require('./lib/schemapack.js').SchemaPack; module.exports = { // Old API wrapping the new API + + /** + * @callback responseCallback + * @param {object} error - For handling error object + * @param {object} result - For handling conversion/validation object + */ + + /** + * @param {object} input - Contains OpenAPI spec specified in YAML/JSON + * @param {object} options - Holds user configurable properties like schemaFaker, requestNameSource, indentCharacter + * @param {responseCallback} cb - For return + * @returns {responseCallback} Callback with conversion results (success/failure) + */ convert: function(input, options, cb) { var schema = new SchemaPack(input, options); @@ -13,16 +26,28 @@ module.exports = { return cb(null, schema.validationResult); }, + /** + * @param {object} input - Contains OpenAPI spec specified in YAML/JSON + * @returns {object} Object with success/failure status of schema validation along with its reason + */ validate: function (input) { var schema = new SchemaPack(input); return schema.validationResult; }, + /** + * @param {object} input - Contains OpenAPI spec specified in YAML/JSON + * @param {responseCallback} cb - For return + * @returns {responseCallback} Callback with success/failure status of schema validation along with its reason + */ mergeAndValidate: function (input, cb) { var schema = new SchemaPack(input); schema.mergeAndValidate(cb); }, + /** + * @returns {object} Options object with configurable kv-pairs + */ getOptions: function() { return SchemaPack.getOptions(); }, diff --git a/lib/error.js b/lib/error.js index 91d83734f..3cefd2e04 100644 --- a/lib/error.js +++ b/lib/error.js @@ -2,6 +2,7 @@ * constructor openApiErr * @constructor * @param {*} message errorMessage + * @param {*} data - Error object */ function openApiErr(message, data) { this.message = message || ''; diff --git a/lib/parse.js b/lib/parse.js index bd3687d0b..1ef9ba4af 100644 --- a/lib/parse.js +++ b/lib/parse.js @@ -5,6 +5,10 @@ var yaml = require('js-yaml'), module.exports = { + /** Converts json spec to JS object + * @param {String} spec OpenAPI input in string + * @returns {Object} JavaScript object from JSON OpenAPI input + */ asJson: function (spec) { try { return JSON.parse(spec); @@ -14,6 +18,10 @@ module.exports = { } }, + /** Converts yaml spec to JS object + * @param {String} spec OpenAPI input in string + * @returns {Object} JavaScript object from YAML OpenAPI input + */ asYaml: function (spec) { try { return yaml.safeLoad(spec); diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index b9aa25c2c..db342169b 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -95,7 +95,7 @@ schemaFaker.option({ /** * - * @param {*} input - input string that needs to be hashed + * @param {string} input - input string that needs to be hashed * @returns {*} sha1 hash of the string */ function hash(input) { @@ -325,6 +325,9 @@ module.exports = { * OperationParams take precedence over pathParams * @param {array} operationParam operation (Postman request)-level params. * @param {array} pathParam are path parent-level params. + * @param {object} components - components defined in the OAS spec. These are used to + * resolve references while generating params. + * @param {object} options - a standard list of options that's globally passed around. Check options.js for more.) * @returns {*} combined requestParams from operation and path params. */ getRequestParams: function(operationParam, pathParam, components, options) { @@ -379,6 +382,7 @@ module.exports = { /** * Generates a Trie-like folder structure from the root path object of the OpenAPI specification. * @param {Object} spec - specification in json format + * @param {object} options - a standard list of options that's globally passed around. Check options.js for more.) * @returns {Object} - The final object consists of the tree structure and collection variables */ generateTrieFromPaths: function (spec, options) { @@ -409,6 +413,11 @@ module.exports = { // returns a list of methods supported at each pathItem // some pathItem props are not methods // https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#pathItemObject + + /** + * @param {Array} pathKeys - Keys in a path object + * @returns {Array} - Method names available for the path object + */ getPathMethods = function(pathKeys) { var methods = []; // TODO: Show warning for incorrect schema if !pathKeys @@ -522,8 +531,8 @@ module.exports = { * @param {object|array} commonPathVars - Object of path variables taken from the specification * @param {object} components - components defined in the OAS spec. These are used to * resolve references while generating params. - * @param {object} options - a standard list of options that's globally passed around. Check options.js for more. - * @param {object} schemaCache - object storing schemaFaker and schmeResolution caches + * @param {object} options - a standard list of options that's globally passed around. Check options.js for more. + * @param {object} schemaCache - object storing schemaFaker and schmeResolution caches * @returns {Array} returns an array of sdk.Variable */ convertPathVariables: function(type, providedPathVars, commonPathVars, components, options, schemaCache) { @@ -804,6 +813,9 @@ module.exports = { /** * returns first example in the input map * @param {*} exampleObj map[string, exampleObject] + * @param {object} components - components defined in the OAS spec. These are used to + * resolve references while generating params. + * @param {object} options - a standard list of options that's globally passed around. Check options.js for more.) * @returns {*} first example in the input map type */ getExampleData: function(exampleObj, components, options) { @@ -840,8 +852,8 @@ module.exports = { * @param {string} indentCharacter is needed for XML/JSON bodies only * @param {object} components - components defined in the OAS spec. These are used to * resolve references while generating params. - * @param {object} options - a standard list of options that's globally passed around. Check options.js for more. - * @param {object} schemaCache - object storing schemaFaker and schmeResolution caches + * @param {object} options - a standard list of options that's globally passed around. Check options.js for more. + * @param {object} schemaCache - object storing schemaFaker and schmeResolution caches * @returns {*} postman body data */ // TODO: We also need to accept the content type @@ -1453,6 +1465,9 @@ module.exports = { /** * @param {*} $ref reference object + * @param {object} components - components defined in the OAS spec. These are used to + * resolve references while generating params. + * @param {object} options - a standard list of options that's globally passed around. Check options.js for more.) * @returns {Object} reference object from the saved components * @no-unit-tests */ @@ -1750,6 +1765,12 @@ module.exports = { // along with the path object, this also returns the values of the // path variable's values // also, any endpoint-level params are merged into the returned pathItemObject + /** + * @param {*} method http method of a Postman transaction request object + * @param {*} url stringified url of a Postman transaction request object + * @param {*} schema takes an openapi schema object + * @returns {array} array of path objects + */ findMatchingRequestFromSchema: function (method, url, schema) { // first step - get array of requests from schema let parsedUrl = require('url').parse(url), @@ -1833,7 +1854,7 @@ module.exports = { ); retVal.push({ - // using path instead of operationId / sumamry since it's widely understood + // using path instead of operationId / summary since it's widely understood name: method + ' ' + path, path: matchedPath, jsonPath: matchedPathJsonPath + '.' + method.toLowerCase(), @@ -2078,6 +2099,17 @@ module.exports = { }); }, + /** + * + * @param {*} requestUrl stringified url of a Postman transaction request object + * @param {*} transactionPathPrefix the jsonpath for this validation (will be prepended to all identified mismatches) + * @param {*} schemaPath the applicable pathItem defined at the schema level + * @param {*} components the components + paths from the OAS spec that need to be used to resolve $refs + * @param {*} options OAS options + * @param {*} schemaResolutionCache cache used to store resolved schemas + * @param {*} callback Callback + * @returns {array} mismatches (in the callback) + */ checkQueryParams(requestUrl, transactionPathPrefix, schemaPath, components, options, schemaResolutionCache, callback) { let parsedUrl = require('url').parse(requestUrl), @@ -2173,6 +2205,17 @@ module.exports = { }); }, + /** + * + * @param {*} headers headers for a Postman transaction request object + * @param {*} transactionPathPrefix the jsonpath for this validation (will be prepended to all identified mismatches) + * @param {*} schemaPath the applicable pathItem defined at the schema level + * @param {*} components the components + paths from the OAS spec that need to be used to resolve $refs + * @param {*} options OAS options + * @param {*} schemaResolutionCache cache used to store resolved schemas + * @param {*} callback Callback + * @returns {array} mismatches (in the callback) + */ checkRequestHeaders: function (headers, transactionPathPrefix, schemaPath, components, options, schemaResolutionCache, callback) { let schemaHeaders = _.filter(schemaPath.parameters, (param) => { return param.in === 'header'; }), @@ -2234,6 +2277,18 @@ module.exports = { }); }, + /** + * + * @param {*} schemaResponse response against a schema (extracted from schemaPath) + * @param {*} headers headers for a response in set of responses for Postman transaction request + * @param {*} transactionPathPrefix the jsonpath for this validation + * @param {*} schemaPathPrefix prepended to all JSON schema paths on the schema + * @param {*} components the components + paths from the OAS spec that need to be used to resolve $refs + * @param {*} options OAS options + * @param {*} schemaResolutionCache cache used to store resolved schemas + * @param {*} callback Callback + * @returns {array} mismatches (in the callback) + */ checkResponseHeaders: function (schemaResponse, headers, transactionPathPrefix, schemaPathPrefix, components, options, schemaResolutionCache, callback) { // 0. Need to find relevant response from schemaPath.responses @@ -2308,6 +2363,18 @@ module.exports = { }, // Only application/json is validated for now + /** + * + * @param {*} requestBody body of a Postman transaction request object + * @param {*} transactionPathPrefix the jsonpath for this validation (will be prepended to all identified mismatches) + * @param {*} schemaPathPrefix prepended to all JSON schema paths on the schema + * @param {*} schemaPath the applicable pathItem defined at the schema level + * @param {*} components the components + paths from the OAS spec that need to be used to resolve $refs + * @param {*} options OAS options + * @param {*} schemaResolutionCache cache used to store resolved schemas + * @param {*} callback Callback + * @returns {array} mismatches (in the callback) + */ checkRequestBody: function (requestBody, transactionPathPrefix, schemaPathPrefix, schemaPath, components, options, schemaResolutionCache, callback) { // check for body modes @@ -2363,6 +2430,18 @@ module.exports = { return callback(null, []); }, + /** + * + * @param {*} schemaResponse response against a schema (extracted from schemaPath) + * @param {*} body response body in a set of responses for Postman transaction request + * @param {*} transactionPathPrefix the jsonpath for this validation + * @param {*} schemaPathPrefix prepended to all JSON schema paths on the schema + * @param {*} components the components + paths from the OAS spec that need to be used to resolve $refs + * @param {*} options OAS options + * @param {*} schemaResolutionCache cache used to store resolved schemas + * @param {*} callback Callback + * @returns {array} mismatches (in the callback) + */ checkResponseBody: function (schemaResponse, body, transactionPathPrefix, schemaPathPrefix, components, options, schemaResolutionCache, callback) { let schemaContent = _.get(schemaResponse, ['content', 'application/json', 'schema']), @@ -2400,6 +2479,18 @@ module.exports = { }, 0); }, + /** + * + * @param {*} responses set of responses for a Postman transaction request + * @param {*} transactionPathPrefix the jsonpath for this validation + * @param {*} schemaPathPrefix prepended to all JSON schema paths on the schema + * @param {*} schemaPath the applicable pathItem defined at the schema level + * @param {*} components the components + paths from the OAS spec that need to be used to resolve $refs + * @param {*} options OAS options + * @param {*} schemaResolutionCache cache used to store resolved schemas + * @param {*} cb Callback + * @returns {array} mismatches (in the callback) + */ checkResponses: function (responses, transactionPathPrefix, schemaPathPrefix, schemaPath, components, options, schemaResolutionCache, cb) { // responses is an array of repsonses recd. for one Postman request @@ -2499,8 +2590,8 @@ module.exports = { }, /** - * @param {*} pmSuffix - * @param {*} schemaPath + * @param {array} pmSuffix - Array of '/' separated path segments from schema path from OAS spec + * @param {array} schemaPath - Array of all possible suffixes of parsed path from Postman request * @returns {*} score - null of no match, int for match. higher value indicates better match * You get points for the number of URL segments that match * You are penalized for the number of schemaPath segments that you skipped diff --git a/lib/schemapack.js b/lib/schemapack.js index 5ada66160..612e8cfe1 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -76,6 +76,10 @@ class SchemaPack { } // need to store the schema here + /** + * @returns {object} - Object with schema validation results (true/false), + * reason (if false) and error (if any) + */ validate() { let input = this.input, json, @@ -147,6 +151,16 @@ class SchemaPack { return this.validationResult; } + /** + * @callback responseCallback + * @param {object} error - For handling error object + * @param {object} result - For handling conversion/validation object + */ + + /** + * @param {responseCallback} cb - For return + * @returns {responseCallback} Callback with success/failure status of schema validation + */ mergeAndValidate (cb) { let input = this.input, validationResult, @@ -198,6 +212,10 @@ class SchemaPack { // convert method, this is called when you want to convert a schema that you've already loaded // in the constructor + /** + * @param {responseCallback} callback - For return + * @returns {responseCallback} Callback with conversion results (success/failure) + */ convert (callback) { let openapi, generatedStore = {}, @@ -464,6 +482,9 @@ class SchemaPack { }, 0); } + /** + * @returns {object} options object to used as kv-pairs + */ static getOptions() { return getOptions(); } diff --git a/lib/utils.js b/lib/utils.js index f4c03ffe9..669a5bca0 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,7 +1,12 @@ // this will have non-OAS-related utils module.exports = { - // merge userOptions over defaultOptions + + /** Merge userOptions over defaultOptions + * @param {Object} defaultOptions - Object with all default options + * @param {Object} userOptions - Object with user specified options + * @returns {Object} Object with kv-pairs of option IDs and their specified/default values + */ mergeOptions: function (defaultOptions, userOptions) { let retVal = {}; diff --git a/lib/xmlSchemaFaker.js b/lib/xmlSchemaFaker.js index a988fa644..db2c48234 100644 --- a/lib/xmlSchemaFaker.js +++ b/lib/xmlSchemaFaker.js @@ -1,6 +1,14 @@ /* eslint-disable */ const _ = require('lodash'); +/** +* Converts an openapi spec schema to a fake XML standard +* @param {string} name - Name tag of the schema +* @param {*} schema - The openapi schema to fake +* @param {string} indentChar - Char for 1 unit of indentation +* @param {number} inden - Extent of indentation desired, specified in units +* @returns {string} - Generated fake XML string +*/ function convertSchemaToXML(name, schema, attribute, indentChar, inden) { var tagPrefix = '', cInden = _.times(inden, _.constant(indentChar)).join(''); From c0ca21d7a0794fb3ee8e2d00bcdc8270c4375797 Mon Sep 17 00:00:00 2001 From: Zomato Date: Sun, 5 Apr 2020 04:08:16 +0530 Subject: [PATCH 2/2] added description for methods --- index.js | 9 +++++++-- lib/schemaUtils.js | 21 +++++++++++++-------- lib/schemapack.js | 7 +++++-- 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index 87454f053..36597a100 100644 --- a/index.js +++ b/index.js @@ -3,15 +3,17 @@ const SchemaPack = require('./lib/schemapack.js').SchemaPack; module.exports = { - // Old API wrapping the new API - /** + * @description - Global callback type declaration * @callback responseCallback * @param {object} error - For handling error object * @param {object} result - For handling conversion/validation object */ + // Old API wrapping the new API + /** + * @description - Converts OpenAPI spec to a Postman collection * @param {object} input - Contains OpenAPI spec specified in YAML/JSON * @param {object} options - Holds user configurable properties like schemaFaker, requestNameSource, indentCharacter * @param {responseCallback} cb - For return @@ -27,6 +29,7 @@ module.exports = { }, /** + * @description - Checks that input is valid YAML/JSON * @param {object} input - Contains OpenAPI spec specified in YAML/JSON * @returns {object} Object with success/failure status of schema validation along with its reason */ @@ -36,6 +39,7 @@ module.exports = { }, /** + * @description - Checks JSON/YAML input in file hierarchy with a root dir * @param {object} input - Contains OpenAPI spec specified in YAML/JSON * @param {responseCallback} cb - For return * @returns {responseCallback} Callback with success/failure status of schema validation along with its reason @@ -46,6 +50,7 @@ module.exports = { }, /** + * @description - Config options as kv-pairs passed to convert method * @returns {object} Options object with configurable kv-pairs */ getOptions: function() { diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index db342169b..d7163f937 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -844,7 +844,7 @@ module.exports = { }, /** - * converts one of the eamples or schema in Media Type object to postman data + * converts one of the examples or schema in Media Type object to postman data * @param {*} bodyObj is MediaTypeObject * @param {*} requestType - Specifies whether the request body is of example request or root request * @param {*} contentType - content type header @@ -1389,6 +1389,7 @@ module.exports = { }, /** + * converts operation item response to a Postman response * @param {*} response in operationItem responses * @param {*} code - response Code * @param {*} originalRequest - the request for the example @@ -1870,7 +1871,8 @@ module.exports = { }, /** - * + * @description - validates value of the specified property against the schema passed in arguments + * and returns array of mismatches (containing property, paths & reason) * @param {*} property - one of QUERYPARAM, PATHVARIABLE, HEADER, REQUEST_BODY, RESPONSE_HEADER, RESPONSE_BODY * @param {*} jsonPathPrefix - this will be prepended to all JSON schema paths on the request * @param {*} txnParamName - Optional - The name of the param being validated (useful for query params, @@ -2005,6 +2007,7 @@ module.exports = { /** * + * @description - validates path variables of request schema (from Postman) and returns mismatches * @param {*} determinedPathVariables the key/determined-value pairs of the path variables (from Postman) * @param {*} transactionPathPrefix the jsonpath for this validation (will be prepended to all identified mismatches) * @param {*} schemaPath the applicable pathItem defined at the schema level @@ -2100,7 +2103,7 @@ module.exports = { }, /** - * + * @description - validates query params of a Postman request object and returns mismatches * @param {*} requestUrl stringified url of a Postman transaction request object * @param {*} transactionPathPrefix the jsonpath for this validation (will be prepended to all identified mismatches) * @param {*} schemaPath the applicable pathItem defined at the schema level @@ -2206,7 +2209,7 @@ module.exports = { }, /** - * + * @description - validates headers in headers of a Postman request object and returns mismatches * @param {*} headers headers for a Postman transaction request object * @param {*} transactionPathPrefix the jsonpath for this validation (will be prepended to all identified mismatches) * @param {*} schemaPath the applicable pathItem defined at the schema level @@ -2278,7 +2281,7 @@ module.exports = { }, /** - * + * @description - validates response headers of a schema response and returns mismatches * @param {*} schemaResponse response against a schema (extracted from schemaPath) * @param {*} headers headers for a response in set of responses for Postman transaction request * @param {*} transactionPathPrefix the jsonpath for this validation @@ -2364,7 +2367,7 @@ module.exports = { // Only application/json is validated for now /** - * + * @description - validates body of a Postman request object and returns mismatches * @param {*} requestBody body of a Postman transaction request object * @param {*} transactionPathPrefix the jsonpath for this validation (will be prepended to all identified mismatches) * @param {*} schemaPathPrefix prepended to all JSON schema paths on the schema @@ -2431,7 +2434,7 @@ module.exports = { }, /** - * + * @description - validates response of a schema and returns mismatches * @param {*} schemaResponse response against a schema (extracted from schemaPath) * @param {*} body response body in a set of responses for Postman transaction request * @param {*} transactionPathPrefix the jsonpath for this validation @@ -2480,7 +2483,7 @@ module.exports = { }, /** - * + * @description - validates responses of a Postman request object and returns mismatches * @param {*} responses set of responses for a Postman transaction request * @param {*} transactionPathPrefix the jsonpath for this validation * @param {*} schemaPathPrefix prepended to all JSON schema paths on the schema @@ -2541,6 +2544,8 @@ module.exports = { }, /** + * @description - Determines whether path to a postman transaction request object & the schema path match. + * If they do, method calculates a similarity score * @param {string} postmanPath - parsed path (exclude host and params) from the Postman request * @param {string} schemaPath - schema path from the OAS spec (exclude servers object) * @returns {*} score + match + pathVars - higher score - better match. null - no match diff --git a/lib/schemapack.js b/lib/schemapack.js index 612e8cfe1..13183d546 100644 --- a/lib/schemapack.js +++ b/lib/schemapack.js @@ -30,8 +30,8 @@ const COLLECTION_NAME = 'Converted from OpenAPI', * @param {object} generatedStore - the store that holds the generated collection. Modified in-place * @param {object} components - components defined in the OAS spec. These are used to * resolve references while generating params. - * @param {object} options - a standard list of options that's globally passed around. Check options.js for more. - * @param {object} schemaCache - object storing schemaFaker and schmeResolution caches + * @param {object} options - a standard list of options that's globally passed around. Check options.js for more. + * @param {object} schemaCache - object storing schemaFaker and schmeResolution caches * @returns {void} - generatedStore is modified in-place */ generateCollection = function (specWrapper, generatedStore, components, options, schemaCache) { @@ -76,6 +76,8 @@ class SchemaPack { } // need to store the schema here + + // Checks that input is valid YAML/JSON /** * @returns {object} - Object with schema validation results (true/false), * reason (if false) and error (if any) @@ -157,6 +159,7 @@ class SchemaPack { * @param {object} result - For handling conversion/validation object */ + // Checks JSON/YAML input in file hierarchy with a root dir /** * @param {responseCallback} cb - For return * @returns {responseCallback} Callback with success/failure status of schema validation