Skip to content

Commit 7de8414

Browse files
committed
Avoid missing deprecated in validateTransaction
1 parent 5dbe0f7 commit 7de8414

File tree

5 files changed

+778
-18
lines changed

5 files changed

+778
-18
lines changed

lib/schemaUtils.js

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ function safeSchemaFaker(oldSchema, resolveTo, resolveFor, parameterSourceOption
232232
/**
233233
* Verifies if the deprecated operations should be added
234234
*
235-
* @param {object} tempRequest - openAPI spec object
235+
* @param {object} operation - openAPI operation object
236236
* @param {object} options - a standard list of options that's globally passed around. Check options.js for more.
237237
* @returns {boolean} whether to add or not the deprecated operation
238238
*/
239-
function shouldAddDeprecatedOperation (tempRequest, options) {
240-
return !tempRequest.properties.deprecated ||
241-
(tempRequest.properties.deprecated === true && options.includeDeprecated === true);
239+
function shouldAddDeprecatedOperation (operation, options) {
240+
return !operation.deprecated ||
241+
(operation.deprecated === true && options.includeDeprecated === true);
242242
}
243243

244244

@@ -884,7 +884,7 @@ module.exports = {
884884
type: 'item',
885885
servers: pathLevelServers || undefined
886886
};
887-
if (shouldAddDeprecatedOperation(tempRequest, options)) {
887+
if (shouldAddDeprecatedOperation(tempRequest.properties, options)) {
888888
generatedStore.collection.items.add(this.convertRequestToItem(
889889
spec, tempRequest, components, options, schemaCache, variableStore));
890890
}
@@ -921,7 +921,7 @@ module.exports = {
921921
});
922922

923923
_.forEach(tagFolder.requests, (request) => {
924-
if (shouldAddDeprecatedOperation(request, options)) {
924+
if (shouldAddDeprecatedOperation(request.properties, options)) {
925925
itemGroup.items.add(
926926
this.convertRequestToItem(spec, request, components, options, schemaCache, variableStore));
927927
}
@@ -1045,7 +1045,7 @@ module.exports = {
10451045
// and add as children to this folder
10461046
for (i = 0, requestCount = resource.requests.length; i < requestCount; i++) {
10471047

1048-
if (shouldAddDeprecatedOperation(resource.requests[i], options)) {
1048+
if (shouldAddDeprecatedOperation(resource.requests[i].properties, options)) {
10491049
itemGroup.items.add(
10501050
this.convertRequestToItem(openapi, resource.requests[i], components, options,
10511051
schemaCache, variableStore, fromWebhooks)
@@ -1071,7 +1071,7 @@ module.exports = {
10711071

10721072
// 2. it has only 1 direct request of its own
10731073
if (resource.requests.length === 1) {
1074-
if (shouldAddDeprecatedOperation(resource.requests[0], options)) {
1074+
if (shouldAddDeprecatedOperation(resource.requests[0].properties, options)) {
10751075
return this.convertRequestToItem(openapi, resource.requests[0], components, options,
10761076
schemaCache, variableStore, fromWebhooks);
10771077
}
@@ -4911,7 +4911,10 @@ module.exports = {
49114911
_.forEach(schemaPaths, (schemaPathObj, schemaPath) => {
49124912
_.forEach(_.keys(schemaPathObj), (pathKey) => {
49134913
schemaJsonPath = `$.paths[${schemaPath}].${_.toLower(pathKey)}`;
4914-
if (METHODS.includes(pathKey) && !matchedEndpoints.includes(schemaJsonPath)) {
4914+
let operationItem = _.get(schemaPathObj, pathKey) || {},
4915+
shouldValidateDeprecated = shouldAddDeprecatedOperation(operationItem, options);
4916+
if (METHODS.includes(pathKey) && !matchedEndpoints.includes(schemaJsonPath) &&
4917+
shouldValidateDeprecated) {
49154918
let mismatchObj = {
49164919
property: 'ENDPOINT',
49174920
transactionJsonPath: null,

test/unit/validator.test.js

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1714,3 +1714,183 @@ describe('validateTransaction convert and validate schemas with allOf', function
17141714
});
17151715
});
17161716

1717+
describe('validateTransaction convert and validate schemas with deprecated elements', function () {
1718+
it('Should convert and validate and include deprecated operation default option', function () {
1719+
const openAPI = path.join(__dirname, VALID_OPENAPI_FOLDER_PATH + '/has_some_op_dep.json'),
1720+
openAPIData = fs.readFileSync(openAPI, 'utf8'),
1721+
options = {
1722+
showMissingInSchemaErrors: true,
1723+
strictRequestMatching: true,
1724+
ignoreUnresolvedVariables: true
1725+
},
1726+
schemaPack = new Converter.SchemaPack({ type: 'string', data: openAPIData }, options);
1727+
schemaPack.convert((err, conversionResult) => {
1728+
expect(err).to.be.null;
1729+
expect(conversionResult.result).to.equal(true);
1730+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1731+
1732+
let historyRequest = [];
1733+
1734+
getAllTransactions(conversionResult.output[0].data, historyRequest);
1735+
1736+
schemaPack.validateTransaction(historyRequest, (err, result) => {
1737+
expect(err).to.be.null;
1738+
expect(result).to.be.an('object');
1739+
1740+
expect(err).to.be.null;
1741+
expect(result.missingEndpoints.length).to.eq(0);
1742+
1743+
let requestIds = Object.keys(result.requests);
1744+
requestIds.forEach((requestId) => {
1745+
expect(result.requests[requestId].endpoints[0]).to.not.be.undefined;
1746+
expect(result.requests[requestId].endpoints[0].matched).to.be.true;
1747+
});
1748+
});
1749+
});
1750+
});
1751+
1752+
it('Should convert and validate and include deprecated operation', function () {
1753+
const openAPI = path.join(__dirname, VALID_OPENAPI_FOLDER_PATH + '/has_some_op_dep.json'),
1754+
openAPIData = fs.readFileSync(openAPI, 'utf8'),
1755+
options = {
1756+
showMissingInSchemaErrors: true,
1757+
strictRequestMatching: true,
1758+
ignoreUnresolvedVariables: true,
1759+
includeDeprecated: true
1760+
},
1761+
schemaPack = new Converter.SchemaPack({ type: 'string', data: openAPIData }, options);
1762+
schemaPack.convert((err, conversionResult) => {
1763+
expect(err).to.be.null;
1764+
expect(conversionResult.result).to.equal(true);
1765+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1766+
1767+
let historyRequest = [];
1768+
1769+
getAllTransactions(conversionResult.output[0].data, historyRequest);
1770+
1771+
schemaPack.validateTransaction(historyRequest, (err, result) => {
1772+
expect(err).to.be.null;
1773+
expect(result).to.be.an('object');
1774+
1775+
expect(err).to.be.null;
1776+
expect(result.missingEndpoints.length).to.eq(0);
1777+
1778+
let requestIds = Object.keys(result.requests);
1779+
requestIds.forEach((requestId) => {
1780+
expect(result.requests[requestId].endpoints[0]).to.not.be.undefined;
1781+
expect(result.requests[requestId].endpoints[0].matched).to.be.true;
1782+
});
1783+
});
1784+
});
1785+
});
1786+
1787+
it('Should convert and validate not including deprecated operation and no missing endpoint', function () {
1788+
const openAPI = path.join(__dirname, VALID_OPENAPI_FOLDER_PATH + '/has_some_op_dep.json'),
1789+
openAPIData = fs.readFileSync(openAPI, 'utf8'),
1790+
options = {
1791+
showMissingInSchemaErrors: true,
1792+
strictRequestMatching: true,
1793+
ignoreUnresolvedVariables: true,
1794+
includeDeprecated: false
1795+
},
1796+
schemaPack = new Converter.SchemaPack({ type: 'string', data: openAPIData }, options);
1797+
schemaPack.convert((err, conversionResult) => {
1798+
expect(err).to.be.null;
1799+
expect(conversionResult.result).to.equal(true);
1800+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1801+
1802+
let historyRequest = [];
1803+
1804+
getAllTransactions(conversionResult.output[0].data, historyRequest);
1805+
1806+
schemaPack.validateTransaction(historyRequest, (err, result) => {
1807+
expect(err).to.be.null;
1808+
expect(result).to.be.an('object');
1809+
1810+
expect(err).to.be.null;
1811+
expect(result.missingEndpoints.length).to.eq(0);
1812+
let requestIds = Object.keys(result.requests);
1813+
requestIds.forEach((requestId) => {
1814+
expect(result.requests[requestId].endpoints[0]).to.not.be.undefined;
1815+
expect(result.requests[requestId].endpoints[0].matched).to.be.true;
1816+
});
1817+
});
1818+
});
1819+
});
1820+
1821+
it('Should convert and validate including deprecated operation and report mismatch' +
1822+
'when missing', function () {
1823+
const openAPI = path.join(__dirname, VALID_OPENAPI_FOLDER_PATH + '/has_some_op_dep.json'),
1824+
openAPIData = fs.readFileSync(openAPI, 'utf8'),
1825+
options = {
1826+
showMissingInSchemaErrors: true,
1827+
strictRequestMatching: true,
1828+
ignoreUnresolvedVariables: true,
1829+
includeDeprecated: true
1830+
},
1831+
schemaPack = new Converter.SchemaPack({ type: 'string', data: openAPIData }, options);
1832+
schemaPack.convert((err, conversionResult) => {
1833+
expect(err).to.be.null;
1834+
expect(conversionResult.result).to.equal(true);
1835+
expect(conversionResult.output[0].data.item.length).to.equal(1);
1836+
1837+
let historyRequest = [];
1838+
1839+
getAllTransactions(conversionResult.output[0].data, historyRequest);
1840+
1841+
historyRequest.shift();
1842+
schemaPack.validateTransaction(historyRequest, (err, result) => {
1843+
expect(err).to.be.null;
1844+
expect(result).to.be.an('object');
1845+
1846+
expect(err).to.be.null;
1847+
expect(result.missingEndpoints.length).to.eq(1);
1848+
expect(result.missingEndpoints[0].endpoint).to.eq('GET /pets');
1849+
let requestIds = Object.keys(result.requests);
1850+
requestIds.forEach((requestId) => {
1851+
expect(result.requests[requestId].endpoints[0]).to.not.be.undefined;
1852+
expect(result.requests[requestId].endpoints[0].matched).to.be.true;
1853+
});
1854+
});
1855+
});
1856+
});
1857+
1858+
it('Should convert and validate including deprecated operation and validate when deprecated is present', function () {
1859+
const openAPI = path.join(__dirname, VALID_OPENAPI_FOLDER_PATH + '/has_some_op_dep.json'),
1860+
openAPIData = fs.readFileSync(openAPI, 'utf8'),
1861+
options = {
1862+
showMissingInSchemaErrors: true,
1863+
strictRequestMatching: true,
1864+
ignoreUnresolvedVariables: true,
1865+
includeDeprecated: true
1866+
},
1867+
schemaPack = new Converter.SchemaPack({ type: 'string', data: openAPIData }, options);
1868+
schemaPack.convert((err, conversionResult) => {
1869+
expect(err).to.be.null;
1870+
expect(conversionResult.result).to.equal(true);
1871+
1872+
let historyRequest = [],
1873+
optionsOtherSchemaPack = {
1874+
showMissingInSchemaErrors: true,
1875+
strictRequestMatching: true,
1876+
ignoreUnresolvedVariables: true,
1877+
includeDeprecated: false
1878+
},
1879+
schemaPack2 = new Converter.SchemaPack({ type: 'string', data: openAPIData }, optionsOtherSchemaPack);
1880+
1881+
getAllTransactions(conversionResult.output[0].data, historyRequest);
1882+
1883+
schemaPack2.validateTransaction(historyRequest, (err, result) => {
1884+
expect(err).to.be.null;
1885+
expect(result).to.be.an('object');
1886+
1887+
expect(err).to.be.null;
1888+
let requestIds = Object.keys(result.requests);
1889+
requestIds.forEach((requestId) => {
1890+
expect(result.requests[requestId].endpoints[0]).to.not.be.undefined;
1891+
expect(result.requests[requestId].endpoints[0].matched).to.be.true;
1892+
});
1893+
});
1894+
});
1895+
});
1896+
});

0 commit comments

Comments
 (0)