resolve matchingrequest when is a webhook

This commit is contained in:
Luis Tejeda
2022-01-24 16:39:50 -06:00
parent 8a2e0f2848
commit bfaf814518
5 changed files with 4913 additions and 2 deletions

View File

@@ -2504,9 +2504,21 @@ module.exports = {
pathToMatch,
matchedPath,
matchedPathJsonPath,
schemaPathItems = schema.paths,
schemaPathItems,
filteredPathItemsArray = [];
_.forOwn(schema.paths, (pathItemObject) => {
pathItemObject.kind = 'paths';
});
schemaPathItems = schema.paths;
if (schema.webhooks) {
_.forOwn(schema.webhooks, (webhookItemObject) => {
webhookItemObject.kind = 'webhooks';
});
schemaPathItems = Object.assign(schemaPathItems, schema.webhooks);
}
// Return no matches for invalid url (if unable to decode parsed url)
try {
pathToMatch = decodeURI(parsedUrl.pathname);
@@ -2577,6 +2589,7 @@ module.exports = {
_.each(filteredPathItemsArray, (fp) => {
let path = fp.path,
pathItemObject = fp.pathItem,
kind = pathItemObject.kind,
score = fp.matchScore,
pathVars = fp.pathVars;
@@ -2586,7 +2599,7 @@ module.exports = {
return true;
}
matchedPathJsonPath = `$.paths[${path}]`;
matchedPathJsonPath = `$.${kind}[${path}]`;
// filter empty parameters
matchedPath.parameters = _.reduce(matchedPath.parameters, (accumulator, param) => {

File diff suppressed because one or more lines are too long

File diff suppressed because it is too large Load Diff

View File

@@ -1018,5 +1018,146 @@ describe('VALIDATE FUNCTION TESTS ', function () {
expect(result[1].name).to.eql('GET /lookups');
done();
});
it('Should find matching request when comes from a webhook', function (done) {
let schema = {
paths: {
'/lookups': {
'get': { 'summary': 'Lookup Job Values' }
},
'/{jobid}': {
'get': {
'summary': 'Get Job by ID',
'parameters': [
{
'in': 'path',
'name': 'jobid',
'schema': {
'type': 'string'
},
'required': true,
'description': 'Unique identifier for a job to retrieve.',
'example': '{{jobid}}'
}
]
}
}
},
webhooks: {
'/ACCOUNT_CLOSED': {
post: {
description: 'This notification is sent when an account has been closed.',
operationId: 'post-ACCOUNT_CLOSED',
requestBody: {
content: {
'application/json': {
examples: {
accountClosed: {
$ref: '#/components/examples/post-ACCOUNT_CLOSED-accountClosed'
}
},
schema: {
$ref: '#/components/schemas/AccountCloseNotification'
}
}
}
},
responses: {
'200': {
content: {
'application/json': {
examples: {
accountClosed: {
$ref: '#/components/examples/WebhookAck'
}
},
schema: {
$ref: '#/components/schemas/NotificationResponse'
}
}
},
description: 'OK - the request has succeeded.'
}
},
security: [
{
BasicAuth: [
]
},
{
ApiKeyAuth: [
]
}
],
summary: 'Triggered upon the closure of an account.',
tags: [
'Accounts'
],
'x-groupName': 'Accounts',
'x-sortIndex': 3,
parameters: [
],
schemaPathName: '/ACCOUNT_CLOSED'
},
parameters: [
]
},
'/ACCOUNT_CREATED': {
post: {
description: 'This notification is sent when an account has been created.',
operationId: 'post-ACCOUNT_CREATED',
requestBody: {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/AccountCreateNotification'
}
}
}
},
responses: {
'200': {
content: {
'application/json': {
schema: {
$ref: '#/components/schemas/NotificationResponse'
}
}
},
description: 'OK - the request has succeeded.'
}
},
security: [
{
BasicAuth: [
]
},
{
ApiKeyAuth: [
]
}
],
summary: 'Triggered upon the creation of an account.',
tags: [
'Accounts'
],
'x-groupName': 'Accounts',
'x-sortIndex': 1
},
parameters: [
]
}
}
},
schemaPath = '{{baseUrl}}/ACCOUNT_CLOSED',
result;
result = schemaUtils.findMatchingRequestFromSchema('POST', schemaPath, schema, { strictRequestMatching: true });
expect(result).to.have.lengthOf(1);
expect(result[0].name).to.eql('POST /ACCOUNT_CLOSED');
expect(result[0].jsonPath).to.eql('$.webhooks[/ACCOUNT_CLOSED].post');
done();
});
});
});

View File

@@ -107,6 +107,14 @@ describe('Testing openapi 3.1 schema pack convert', function() {
describe('Openapi 3.1 schema pack validateTransactions', function() {
/**
* @description Takes in a collection and a buffered allRequests array
*
* @param {object} collection a postman collection object
* @param {Array} allRequests array as buffer
* @returns {undefined} nothing
*/
function getAllTransactions (collection, allRequests) {
if (!_.has(collection, 'item') || !_.isArray(collection.item)) {
return;
@@ -120,6 +128,7 @@ describe('Openapi 3.1 schema pack validateTransactions', function() {
}
});
}
it('Should not generate any mismatch with a correct file', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/compositeSchemaCollection.json'),
collectionData = fs.readFileSync(collectionSource, 'utf8'),
@@ -141,6 +150,31 @@ describe('Openapi 3.1 schema pack validateTransactions', function() {
});
});
it('Should not generate any mismatch with a correct file with webhooks', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/simpleCollectionsWithWebhooks.json'),
collectionData = fs.readFileSync(collectionSource, 'utf8'),
schemaSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/simpleCollectionsWithWebhooksSpec.yaml'),
schemaData = fs.readFileSync(schemaSource, 'utf8'),
validator = new SchemaPack({
type: 'string',
data: schemaData
});
let transactions = [],
failRequests = [];
getAllTransactions(JSON.parse(collectionData), transactions);
validator.validateTransaction(transactions, (err, result) => {
let requestIds = Object.keys(result.requests);
// expect(err).to.be.null;
requestIds.forEach((requestId) => {
if (result.requests[requestId].endpoints[0].matched === false) {
failRequests.push(result.requests[requestId]);
}
});
expect(failRequests).to.be.empty;
});
});
it('Should not generate any mismatch with a correct file with null type', function() {
const collectionSource = path.join(__dirname, OPENAPI_31_COLLECTIONS + '/compositeSchemaNullableCollection.json'),
collectionData = fs.readFileSync(collectionSource, 'utf8'),