Merge pull request #415 from postmanlabs/feature/fix-absent-pathvar-validation

Fixed issue where validation was giving INVALID_TYPE instead of MISSING_IN_REQUEST if path variable is not present in transaction.
This commit is contained in:
Ankit Saini
2021-10-06 11:15:46 +05:30
committed by GitHub
5 changed files with 92 additions and 5 deletions

View File

@@ -3245,6 +3245,11 @@ module.exports = {
return cb(null, mismatches);
}
// don't validate variable if not present in transaction
if (!pathVar._varMatched) {
return cb(null, mismatches);
}
// assign parameter example(s) as schema examples
this.assignParameterExamples(schemaPathVar);
@@ -3276,7 +3281,9 @@ module.exports = {
// go through required schemaPathVariables, and params that aren't found in the given transaction are errors
_.each(schemaPathVariables, (pathVar) => {
if (!_.find(determinedPathVariables, (param) => { return param.key === pathVar.name; })) {
if (!_.find(determinedPathVariables, (param) => {
return param.key === pathVar.name && param._varMatched;
})) {
// assign parameter example(s) as schema examples;
this.assignParameterExamples(pathVar);

View File

@@ -447,6 +447,8 @@ class SchemaPack {
return transactionPathVar.key === pathVar.key;
});
pathVar.value = _.get(mappedPathVar, 'value', pathVar.value);
// set _varMatched flag which represents if variable was found in transaction or not
pathVar._varMatched = !_.isEmpty(mappedPathVar);
});
// resolve $ref in all parameter objects if present

View File

@@ -91,6 +91,58 @@
}
],
"event": []
},
{
"id": "82704c56-aadb-4925-a6bf-db07b4a8f538",
"name": "/pets",
"request": {
"name": "/pets",
"description": {},
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": []
},
"method": "GET",
"auth": null
},
"response": [
{
"id": "9a9c0108-1ec7-4ba3-82f2-0c335e650c49",
"name": "Success",
"originalRequest": {
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": []
},
"method": "GET",
"body": {}
},
"status": "OK",
"code": 200,
"header": [
{
"key": "Content-Type",
"value": "text/plain"
}
],
"body": "",
"cookie": [],
"_postman_previewlanguage": "text"
}
],
"event": []
}
],
"event": [],

View File

@@ -45,6 +45,27 @@
}
}
}
},
"/{petId}": {
"get": {
"parameters": [
{
"name": "petId",
"in": "path",
"required": true,
"schema": {
"type": "number",
"format": "double"
},
"example": 99.99
}
],
"responses": {
"200": {
"description": "Success"
}
}
}
}
}
}

View File

@@ -589,12 +589,13 @@ describe('VALIDATE FUNCTION TESTS ', function () {
});
});
it('Should correctly validate schema having path with multiple path variables', function (done) {
it('Should correctly validate schema having path with various path variables', function (done) {
let multiplePathVarSpec = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
'/multiplePathVarSpec.json'), 'utf-8'),
multiplePathVarCollection = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
'/multiplePathVarCollection.json'), 'utf-8'),
resultObj,
resultObj1,
resultObj2,
historyRequest = [],
options = {
showMissingInSchemaErrors: true,
@@ -610,8 +611,12 @@ describe('VALIDATE FUNCTION TESTS ', function () {
expect(err).to.be.null;
expect(result).to.be.an('object');
resultObj = result.requests[historyRequest[0].id].endpoints[0];
expect(resultObj.mismatches).to.have.lengthOf(0);
resultObj1 = result.requests[historyRequest[0].id].endpoints[0];
expect(resultObj1.mismatches).to.have.lengthOf(0);
resultObj2 = result.requests[historyRequest[1].id].endpoints[0];
expect(resultObj2.mismatches).to.have.lengthOf(1);
expect(resultObj2.mismatches[0].reasonCode).to.eql('MISSING_IN_REQUEST');
done();
});
});