Skip to content

Commit 5d8a429

Browse files
authored
fix: prevent crash when parameters is illegally not an array (#104)
1 parent 3cd439b commit 5d8a429

File tree

2 files changed

+44
-4
lines changed
  • src/plugins/validation/2and3/semantic-validators
  • test/plugins/validation/2and3

2 files changed

+44
-4
lines changed

src/plugins/validation/2and3/semantic-validators/paths.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,18 @@ module.exports.validate = function({ resolvedSpec }) {
7373
return param;
7474
});
7575

76-
each(path, (thing, thingName) => {
77-
if (thing && thing.parameters) {
76+
each(path, (operation, operationName) => {
77+
if (
78+
operation &&
79+
operation.parameters &&
80+
Array.isArray(operation.parameters)
81+
) {
7882
availableParameters.push(
79-
...thing.parameters.map((param, i) => {
83+
...operation.parameters.map((param, i) => {
8084
if (!isObject(param)) {
8185
return;
8286
}
83-
param.$$path = `paths.${pathName}.${thingName}.parameters[${i}]`;
87+
param.$$path = `paths.${pathName}.${operationName}.parameters[${i}]`;
8488
return param;
8589
})
8690
);

test/plugins/validation/2and3/paths.js

+36
Original file line numberDiff line numberDiff line change
@@ -352,4 +352,40 @@ describe('validation plugin - semantic - paths', function() {
352352
expect(res.warnings).toEqual([]);
353353
});
354354
});
355+
356+
it('should not crash when `parameters` is not an array', function() {
357+
const spec = {
358+
paths: {
359+
'/resource': {
360+
get: {
361+
operationId: 'listResources',
362+
description: 'operation with bad parameters...',
363+
summary: '...but it should not crash the code',
364+
parameters: {
365+
allOf: [
366+
{
367+
name: 'one',
368+
type: 'string'
369+
},
370+
{
371+
name: 'two',
372+
type: 'string'
373+
}
374+
]
375+
},
376+
responses: {
377+
'200': {
378+
description: 'response'
379+
}
380+
}
381+
}
382+
}
383+
}
384+
};
385+
386+
const res = validate({ resolvedSpec: spec });
387+
// errors/warnings would be caught it parameters-ibm.js
388+
expect(res.errors.length).toBe(0);
389+
expect(res.warnings.length).toBe(0);
390+
});
355391
});

0 commit comments

Comments
 (0)