Add schemaResolutionCache logic for validator functions

This commit is contained in:
shreys7
2020-01-21 13:41:33 +05:30
parent 88989dcc67
commit 4516c0adc8
2 changed files with 27 additions and 20 deletions

View File

@@ -1956,6 +1956,7 @@ module.exports = {
* @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)
*/
@@ -1965,6 +1966,7 @@ module.exports = {
schemaPath,
components,
options,
schemaResolutionCache,
callback) {
// schema path should have all parameters needed
@@ -2014,7 +2016,7 @@ module.exports = {
pathVar.key,
pathVar.value,
schemaPathVar.pathPrefix + '[?(@.name==\'' + schemaPathVar.name + '\')]',
deref.resolveRefs(schemaPathVar.schema, 'request', components),
deref.resolveRefs(schemaPathVar.schema, 'request', components, schemaResolutionCache),
components, options, cb);
}, 0);
}, (err, res) => {
@@ -2042,7 +2044,8 @@ module.exports = {
});
},
checkQueryParams(requestUrl, transactionPathPrefix, schemaPath, components, options, callback) {
checkQueryParams(requestUrl, transactionPathPrefix, schemaPath, components, options,
schemaResolutionCache, callback) {
let parsedUrl = require('url').parse(requestUrl),
schemaParams = _.filter(schemaPath.parameters, (param) => { return param.in === 'query'; }),
requestQueryArray = [],
@@ -2101,7 +2104,7 @@ module.exports = {
pQuery.key,
pQuery.value,
schemaParam.pathPrefix + '[?(@.name==\'' + schemaParam.name + '\')]',
deref.resolveRefs(schemaParam.schema, 'request', components),
deref.resolveRefs(schemaParam.schema, 'request', components, schemaResolutionCache),
components, options,
cb
);
@@ -2123,7 +2126,8 @@ module.exports = {
});
},
checkRequestHeaders: function (headers, transactionPathPrefix, schemaPath, components, options, callback) {
checkRequestHeaders: function (headers, transactionPathPrefix, schemaPath, components, options,
schemaResolutionCache, callback) {
let schemaHeaders = _.filter(schemaPath.parameters, (param) => { return param.in === 'header'; }),
mismatchProperty = 'HEADER';
@@ -2161,7 +2165,7 @@ module.exports = {
pHeader.key,
pHeader.value,
schemaHeader.pathPrefix + '[?(@.name==\'' + schemaHeader.name + '\')]',
deref.resolveRefs(schemaHeader.schema, 'request', components),
deref.resolveRefs(schemaHeader.schema, 'request', components, schemaResolutionCache),
components, options,
cb
);
@@ -2184,7 +2188,7 @@ module.exports = {
},
checkResponseHeaders: function (schemaResponse, headers, transactionPathPrefix, schemaPathPrefix,
components, options, callback) {
components, options, schemaResolutionCache, callback) {
// 0. Need to find relevant response from schemaPath.responses
let schemaHeaders,
mismatchProperty = 'RESPONSE_HEADER';
@@ -2230,7 +2234,7 @@ module.exports = {
pHeader.key,
pHeader.value,
schemaPathPrefix + '.headers[' + pHeader.key + ']',
deref.resolveRefs(schemaHeader.schema, 'response', components),
deref.resolveRefs(schemaHeader.schema, 'response', components, schemaResolutionCache),
components,
options,
cb
@@ -2258,7 +2262,7 @@ module.exports = {
// Only application/json is validated for now
checkRequestBody: function (requestBody, transactionPathPrefix, schemaPathPrefix, schemaPath,
components, options, callback) {
components, options, schemaResolutionCache, callback) {
// check for body modes
// TODO: The application/json can be anything that's application/*+json
let jsonSchemaBody = _.get(schemaPath, ['requestBody', 'content', 'application/json', 'schema']),
@@ -2278,7 +2282,7 @@ module.exports = {
try {
ajv = new Ajv({ unknownFormats: ['int32', 'int64'], allErrors: true });
validate = ajv.compile(deref.resolveRefs(jsonSchemaBody, 'request', components));
validate = ajv.compile(deref.resolveRefs(jsonSchemaBody, 'request', components, schemaResolutionCache));
res = validate(JSON.parse(requestBody.raw));
}
catch (e) {
@@ -2313,7 +2317,7 @@ module.exports = {
},
checkResponseBody: function (schemaResponse, body, transactionPathPrefix, schemaPathPrefix,
components, options, callback) {
components, options, schemaResolutionCache, callback) {
let schemaContent = _.get(schemaResponse, ['content', 'application/json', 'schema']),
mismatchProperty = 'RESPONSE_BODY';
@@ -2341,7 +2345,7 @@ module.exports = {
null, // no param name for the request body
body,
schemaPathPrefix + '.content[application/json].schema',
deref.resolveRefs(schemaContent, 'response', components),
deref.resolveRefs(schemaContent, 'response', components, schemaResolutionCache),
components,
_.extend({}, options, { shortValidationErrors: true }),
callback
@@ -2349,7 +2353,8 @@ module.exports = {
}, 0);
},
checkResponses: function (responses, transactionPathPrefix, schemaPathPrefix, schemaPath, components, options, cb) {
checkResponses: function (responses, transactionPathPrefix, schemaPathPrefix, schemaPath,
components, options, schemaResolutionCache, cb) {
// responses is an array of repsonses recd. for one Postman request
// we've already determined the schemaPath against which all responses need to be validated
// loop through all responses
@@ -2375,13 +2380,13 @@ module.exports = {
headers: (cb) => {
this.checkResponseHeaders(thisSchemaResponse, response.header,
transactionPathPrefix + '[' + response.id + ']header',
schemaPathPrefix + '.responses.' + responsePathPrefix, components, options, cb);
schemaPathPrefix + '.responses.' + responsePathPrefix, components, options, schemaResolutionCache, cb);
},
body: (cb) => {
// assume it's JSON at this point
this.checkResponseBody(thisSchemaResponse, response.body,
transactionPathPrefix + '[' + response.id + ']body',
schemaPathPrefix + '.responses.' + responsePathPrefix, components, options, cb);
schemaPathPrefix + '.responses.' + responsePathPrefix, components, options, schemaResolutionCache, cb);
}
}, (err, result) => {
return responseCallback(null, {

View File

@@ -243,7 +243,9 @@ class SchemaPack {
validateTransaction(transactions, callback) {
let schema = this.openapi,
componentsAndPaths,
options = this.computedOptions;
options = this.computedOptions,
schemaResolutionCache = this.schemaFakerCache;
if (!this.validated) {
return callback(new OpenApiErr('The schema must be validated before attempting conversion'));
@@ -303,23 +305,23 @@ class SchemaPack {
async.parallel({
path: function(cb) {
schemaUtils.checkPathVariables(matchedPath.pathVariables, '$.request.url', matchedPath.path,
componentsAndPaths, options, cb);
componentsAndPaths, options, schemaResolutionCache, cb);
},
queryparams: function(cb) {
schemaUtils.checkQueryParams(requestUrl, '$.request.url.query', matchedPath.path,
componentsAndPaths, options, cb);
componentsAndPaths, options, schemaResolutionCache, cb);
},
headers: function(cb) {
schemaUtils.checkRequestHeaders(transaction.request.header, '$.request.header', matchedPath.path,
componentsAndPaths, options, cb);
componentsAndPaths, options, schemaResolutionCache, cb);
},
requestBody: function(cb) {
schemaUtils.checkRequestBody(transaction.request.body, '$.request.body', matchedPath.jsonPath,
matchedPath.path, componentsAndPaths, options, cb);
matchedPath.path, componentsAndPaths, options, schemaResolutionCache, cb);
},
responses: function (cb) {
schemaUtils.checkResponses(transaction.response, '$.responses', matchedPath.jsonPath,
matchedPath.path, componentsAndPaths, options, cb);
matchedPath.path, componentsAndPaths, options, schemaResolutionCache, cb);
}
}, (err, result) => {
let allMismatches = _.concat(result.queryparams, result.headers, result.path, result.requestBody),