Skip to content

Commit c7feb13

Browse files
committed
validate dep query param
1 parent 95ca007 commit c7feb13

File tree

3 files changed

+108
-32
lines changed

3 files changed

+108
-32
lines changed

lib/schemaUtils.js

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3906,33 +3906,39 @@ module.exports = {
39063906
mismatchObj;
39073907

39083908
_.each(_.filter(resolvedSchemaParams, (q) => { return q.required; }), (qp) => {
3909-
if (!_.find(requestQueryParams, (param) => { return param.key === qp.name; })) {
3909+
if (!_.find(requestQueryParams, (param) => {
3910+
return param.key === qp.name;
3911+
})) {
39103912

3911-
// assign parameter example(s) as schema examples;
3912-
this.assignParameterExamples(qp);
3913+
if (shouldAddDeprecatedOperation(qp, options)) {
39133914

3914-
mismatchObj = {
3915-
property: mismatchProperty,
3916-
transactionJsonPath: transactionPathPrefix,
3917-
schemaJsonPath: qp.pathPrefix + '[?(@.name==\'' + qp.name + '\')]',
3918-
reasonCode: 'MISSING_IN_REQUEST',
3919-
reason: `The required query parameter "${qp.name}" was not found in the transaction`
3920-
};
3915+
// assign parameter example(s) as schema examples;
3916+
this.assignParameterExamples(qp);
39213917

3922-
if (options.suggestAvailableFixes) {
3923-
mismatchObj.suggestedFix = {
3924-
key: qp.name,
3925-
actualValue: null,
3926-
suggestedValue: {
3927-
key: qp.name,
3928-
value: safeSchemaFaker(qp.schema || {}, 'example', PROCESSING_TYPE.VALIDATION,
3929-
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
3930-
options.stackLimit, options.includeDeprecated),
3931-
description: this.getParameterDescription(qp)
3932-
}
3918+
mismatchObj = {
3919+
property: mismatchProperty,
3920+
transactionJsonPath: transactionPathPrefix,
3921+
schemaJsonPath: qp.pathPrefix + '[?(@.name==\'' + qp.name + '\')]',
3922+
reasonCode: 'MISSING_IN_REQUEST',
3923+
reason: `The required query parameter "${qp.name}" was not found in the transaction`
39333924
};
3925+
3926+
if (options.suggestAvailableFixes) {
3927+
mismatchObj.suggestedFix = {
3928+
key: qp.name,
3929+
actualValue: null,
3930+
suggestedValue: {
3931+
key: qp.name,
3932+
value: safeSchemaFaker(qp.schema || {}, 'example', PROCESSING_TYPE.VALIDATION,
3933+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache,
3934+
options.stackLimit, options.includeDeprecated),
3935+
description: this.getParameterDescription(qp)
3936+
}
3937+
};
3938+
}
3939+
mismatches.push(mismatchObj);
39343940
}
3935-
mismatches.push(mismatchObj);
3941+
39363942
}
39373943
});
39383944
return callback(null, _.concat(_.flatten(res), mismatches));

test/data/valid_openapi/petstore_deprecated_param.json

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,7 @@
4040
"style": "form",
4141
"explode": false,
4242
"schema": {
43-
"type": ["array"],
44-
"items": {
45-
"type": ["string"],
46-
"examples": ["Hola Mundo"]
47-
}
43+
"type": "string"
4844
}
4945
},
5046
{
@@ -53,12 +49,9 @@
5349
"description": "another random variable",
5450
"style": "spaceDelimited",
5551
"deprecated": true,
52+
"required": true,
5653
"schema": {
57-
"type": ["array"],
58-
"items": {
59-
"type": ["integer"],
60-
"format": "int64"
61-
}
54+
"type": "string"
6255
}
6356
}
6457
],

test/unit/validator.test.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,4 +1893,81 @@ describe('validateTransaction convert and validate schemas with deprecated eleme
18931893
});
18941894
});
18951895
});
1896+
1897+
it('Should convert and validate without error excluding deprecated query param when ' +
1898+
'includeDeprecated is false', function() {
1899+
const openAPI = path.join(__dirname, VALID_OPENAPI_FOLDER_PATH + '/petstore_deprecated_param.json'),
1900+
openAPIData = fs.readFileSync(openAPI, 'utf8'),
1901+
options = {
1902+
showMissingInSchemaErrors: true,
1903+
strictRequestMatching: true,
1904+
ignoreUnresolvedVariables: true,
1905+
includeDeprecated: false
1906+
},
1907+
schemaPack = new Converter.SchemaPack({ type: 'string', data: openAPIData }, options);
1908+
schemaPack.convert((err, conversionResult) => {
1909+
expect(err).to.be.null;
1910+
expect(conversionResult.result).to.equal(true);
1911+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1912+
1913+
let historyRequest = [];
1914+
1915+
getAllTransactions(conversionResult.output[0].data, historyRequest);
1916+
1917+
schemaPack.validateTransaction(historyRequest, (err, result) => {
1918+
expect(err).to.be.null;
1919+
expect(result).to.be.an('object');
1920+
1921+
expect(err).to.be.null;
1922+
expect(result.missingEndpoints.length).to.eq(0);
1923+
let requestIds = Object.keys(result.requests);
1924+
requestIds.forEach((requestId) => {
1925+
expect(result.requests[requestId].endpoints[0]).to.not.be.undefined;
1926+
expect(result.requests[requestId].endpoints[0].matched).to.be.true;
1927+
});
1928+
});
1929+
});
1930+
1931+
});
1932+
1933+
it('Should convert and validate without error excluding deprecated query param when ' +
1934+
'includeDeprecated is true', function() {
1935+
const openAPI = path.join(__dirname, VALID_OPENAPI_FOLDER_PATH + '/petstore_deprecated_param.json'),
1936+
openAPIData = fs.readFileSync(openAPI, 'utf8'),
1937+
options = {
1938+
showMissingInSchemaErrors: true,
1939+
strictRequestMatching: true,
1940+
ignoreUnresolvedVariables: true,
1941+
includeDeprecated: false
1942+
},
1943+
schemaPack = new Converter.SchemaPack({ type: 'string', data: openAPIData }, options);
1944+
schemaPack.convert((err, conversionResult) => {
1945+
expect(err).to.be.null;
1946+
expect(conversionResult.result).to.equal(true);
1947+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1948+
1949+
let historyRequest = [],
1950+
newOptions = {
1951+
showMissingInSchemaErrors: true,
1952+
strictRequestMatching: true,
1953+
ignoreUnresolvedVariables: true,
1954+
includeDeprecated: true
1955+
},
1956+
schemaPack2 = new Converter.SchemaPack({ type: 'string', data: openAPIData }, newOptions);
1957+
1958+
getAllTransactions(conversionResult.output[0].data, historyRequest);
1959+
1960+
schemaPack2.validateTransaction(historyRequest, (err, result) => {
1961+
expect(err).to.be.null;
1962+
expect(result).to.be.an('object');
1963+
1964+
expect(err).to.be.null;
1965+
expect(result.missingEndpoints.length).to.eq(0);
1966+
let requestIds = Object.keys(result.requests);
1967+
expect(result.requests[requestIds[0]].endpoints[0].matched).to.be.false;
1968+
expect(result.requests[requestIds[0]].endpoints[0].mismatches[0].reason.includes('variable2')).to.be.true;
1969+
});
1970+
});
1971+
1972+
});
18961973
});

0 commit comments

Comments
 (0)