Skip to content

Commit 5b0ff9c

Browse files
authored
Merge pull request #497 from postmanlabs/fix496/validateWithServers
match with servers
2 parents aea666b + 3f82eda commit 5b0ff9c

File tree

3 files changed

+610
-2
lines changed

3 files changed

+610
-2
lines changed

lib/schemaUtils.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2611,11 +2611,15 @@ module.exports = {
26112611
matchedPath,
26122612
matchedPathJsonPath,
26132613
schemaPathItems = schema.paths,
2614+
pathToMatchServer,
26142615
filteredPathItemsArray = [];
26152616

26162617
// Return no matches for invalid url (if unable to decode parsed url)
26172618
try {
26182619
pathToMatch = decodeURI(parsedUrl.pathname);
2620+
if (!_.isNil(parsedUrl.hash)) {
2621+
pathToMatch += parsedUrl.hash;
2622+
}
26192623
}
26202624
catch (e) {
26212625
console.warn(
@@ -2654,9 +2658,16 @@ module.exports = {
26542658
}
26552659
return accumulator;
26562660
}, []);
2657-
2661+
let schemaMatchResult = { match: false };
26582662
// check if path and pathToMatch match (non-null)
2659-
let schemaMatchResult = this.getPostmanUrlSchemaMatchScore(pathToMatch, path, options);
2663+
// check in explicit (local defined) servers
2664+
if (pathItemObject[method.toLowerCase()].servers) {
2665+
pathToMatchServer = this.handleExplicitServersPathToMatch(pathToMatch, path);
2666+
schemaMatchResult = this.getPostmanUrlSchemaMatchScore(pathToMatchServer, path, options);
2667+
}
2668+
else {
2669+
schemaMatchResult = this.getPostmanUrlSchemaMatchScore(pathToMatch, path, options);
2670+
}
26602671
if (!schemaMatchResult.match) {
26612672
// there was no reasonable match b/w the postman path and this schema path
26622673
return true;
@@ -4465,6 +4476,32 @@ module.exports = {
44654476
});
44664477
},
44674478

4479+
/**
4480+
* Takes in the postman path and the schema path
4481+
* takes from the path the number of segments present in the schema path
4482+
* and returns the last segments from the path to match in an string format
4483+
*
4484+
* @param {string} pathToMatch - parsed path (exclude host and params) from the Postman request
4485+
* @param {string} schemaPath - schema path from the OAS spec (exclude servers object)
4486+
* @returns {string} only the selected segments from the pathToMatch
4487+
*/
4488+
handleExplicitServersPathToMatch: function (pathToMatch, schemaPath) {
4489+
let pathTMatchSlice,
4490+
schemaPathArr = _.reject(schemaPath.split('/'), (segment) => {
4491+
return segment === '';
4492+
}),
4493+
schemaPathSegments = schemaPathArr.length,
4494+
pathToMatchArr = _.reject(pathToMatch.split('/'), (segment) => {
4495+
return segment === '';
4496+
}),
4497+
pathToMatchSegments = pathToMatchArr.length;
4498+
if (pathToMatchSegments < schemaPathSegments) {
4499+
return pathToMatch;
4500+
}
4501+
pathTMatchSlice = pathToMatchArr.slice(pathToMatchArr.length - schemaPathSegments, pathToMatchArr.length);
4502+
return pathTMatchSlice.join('/');
4503+
},
4504+
44684505
/**
44694506
* @param {string} postmanPath - parsed path (exclude host and params) from the Postman request
44704507
* @param {string} schemaPath - schema path from the OAS spec (exclude servers object)

0 commit comments

Comments
 (0)