This commit is contained in:
Rajneesh Mitharwal 2022-01-02 16:39:59 +05:30 коммит произвёл Mike Kistler
Родитель 14877328b4
Коммит c52215520f
2 изменённых файлов: 12 добавлений и 10 удалений

Просмотреть файл

@ -1,7 +1,7 @@
// Check conformance to Azure parameter order conventions:
// - path parameters must be in the same order as the path
// Note: Missing path parameters will be flagged by the Spectral path-params rule
// NOTE: Missing path parameters will be flagged by the Spectral path-params rule
// `given` is the paths object
module.exports = (paths) => {
@ -23,17 +23,19 @@ module.exports = (paths) => {
const pathItem = paths[pathKey];
const pathItemPathParams = pathItem.parameters?.filter(inPath).map(paramName) ?? [];
// find the first index where in-consistency observed or offset till no in-consistency
// observed to validate further
const indx = pathItemPathParams.findIndex((v, i) => v !== paramsInPath[i]);
// path-params will flag cases where indx >= paramsInPath.length
// If path params exists and are not in expected order then raise the error
if (indx >= 0 && indx < paramsInPath.length) {
// Note: we do not include `indx` in the path because if the parameter is a ref then
// NOTE: we do not include `indx` in the path because if the parameter is a ref then
// Spectral will show the path of the ref'ed parameter and not the path/operation with
// improper ordering
errors.push({
message: `Path parameter "${paramsInPath[indx]}" should appear before "${pathItemPathParams[indx]}".`,
path: ['paths', pathKey, 'parameters'],
path: ['paths', pathKey, 'parameters'], // no index in path
});
} else {
} else { // this will be a case when few path params are defined in respective methods
const offset = pathItemPathParams.length;
methods.filter((m) => pathItem[m]).forEach((method) => {
const opPathParams = pathItem[method].parameters?.filter(inPath).map(paramName) ?? [];
@ -42,7 +44,7 @@ module.exports = (paths) => {
if (indx2 >= 0 && (offset + indx2) < paramsInPath.length) {
errors.push({
message: `Path parameter "${paramsInPath[offset + indx2]}" should appear before "${opPathParams[indx2]}".`,
path: ['paths', pathKey, method, 'parameters'],
path: ['paths', pathKey, method, 'parameters'], // no index in path
});
}
});

Просмотреть файл

@ -62,8 +62,8 @@ test('az-parameter-order should find errors', () => {
};
return linter.run(oasDoc).then((results) => {
expect(results.length).toBe(2);
expect(results[0].path.join('.')).toBe('paths./test1/{p1}/foo/{p2}.parameters.0');
expect(results[1].path.join('.')).toBe('paths./test2/{p1}/foo/{p2}/bar/{p3}.get.parameters.0');
expect(results[0].path.join('.')).toBe('paths./test1/{p1}/foo/{p2}.parameters');
expect(results[1].path.join('.')).toBe('paths./test2/{p1}/foo/{p2}/bar/{p3}.get.parameters');
});
});
@ -191,8 +191,8 @@ test('az-parameter-order should find oas3 errors', () => {
};
return linter.run(oasDoc).then((results) => {
expect(results.length).toBe(2);
expect(results[0].path.join('.')).toBe('paths./test1/{p1}/foo/{p2}.parameters.0');
expect(results[1].path.join('.')).toBe('paths./test2/{p1}/foo/{p2}/bar/{p3}.get.parameters.0');
expect(results[0].path.join('.')).toBe('paths./test1/{p1}/foo/{p2}.parameters');
expect(results[1].path.join('.')).toBe('paths./test2/{p1}/foo/{p2}/bar/{p3}.get.parameters');
});
});