Skip to content

Commit 9282e17

Browse files
SamerJaser96dpopp07
authored andcommitted
fix: add more specific checks for parameter objects (#83)
avoid validating properties named "parameters" as parameters
1 parent e119be3 commit 9282e17

File tree

2 files changed

+107
-2
lines changed

2 files changed

+107
-2
lines changed

src/plugins/validation/2and3/semantic-validators/parameters-ibm.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
2626
return;
2727
}
2828

29-
const contentsOfParameterObject = path[path.length - 2] === 'parameters';
29+
const contentsOfParameterObject = isParameter(path, isOAS3);
3030

31-
// obj is a parameter object
3231
if (contentsOfParameterObject) {
32+
// obj is a parameter object
3333
const isRef = !!obj.$ref;
3434
const hasDescription = !!obj.description;
3535

@@ -157,6 +157,36 @@ module.exports.validate = function({ jsSpec, isOAS3 }, config) {
157157
return { errors: result.error, warnings: result.warning };
158158
};
159159

160+
function isParameter(path, isOAS3) {
161+
const pathsForParameters = [
162+
'get',
163+
'put',
164+
'post',
165+
'delete',
166+
'options',
167+
'head',
168+
'patch',
169+
'trace',
170+
'components'
171+
];
172+
173+
const inParametersSection = path[path.length - 2] === 'parameters';
174+
175+
// the above check is a necessary but not sufficient check for a parameter object
176+
// use the following checks to verify the object is where a parameter is supposed to be.
177+
// without these, a schema property named "parameters" would get validated as a parameter
178+
const isParameterByPath = pathsForParameters.includes(path[path.length - 3]);
179+
const isPathItemParameter =
180+
path[path.length - 4] === 'paths' && path.length === 4;
181+
const isTopLevelParameter =
182+
!isOAS3 && path[0] === 'parameters' && path.length === 2;
183+
184+
return (
185+
inParametersSection &&
186+
(isParameterByPath || isPathItemParameter || isTopLevelParameter)
187+
);
188+
}
189+
160190
function formatValid(obj, isOAS3) {
161191
// References will be checked when the parameters / definitions / components are scanned.
162192
if (obj.$ref || (obj.schema && obj.schema.$ref)) {

test/plugins/validation/2and3/parameters-ibm.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,9 +299,58 @@ describe('validation plugin - semantic - parameters-ibm', () => {
299299
expect(res.errors.length).toEqual(0);
300300
expect(res.warnings.length).toEqual(0);
301301
});
302+
303+
it('should return an error for bad parameters that live in the top level', () => {
304+
const spec = {
305+
parameters: [
306+
{
307+
name: 'someparam',
308+
in: 'header',
309+
type: 'string',
310+
required: true
311+
}
312+
]
313+
};
314+
315+
const res = validate({ jsSpec: spec, isOAS3: false }, config);
316+
expect(res.errors.length).toEqual(1);
317+
expect(res.errors[0].message).toEqual(
318+
'Parameter objects must have a `description` field.'
319+
);
320+
expect(res.warnings.length).toEqual(0);
321+
});
302322
});
303323

304324
describe('OpenAPI 3', () => {
325+
it('should not complain about a property named parameters that is not a parameter object', () => {
326+
const spec = {
327+
components: {
328+
responses: {
329+
parameters: {
330+
description: 'successful operation',
331+
content: {
332+
'application/json': {
333+
schema: {
334+
type: 'object',
335+
properties: {
336+
parameters: {
337+
type: 'string',
338+
description: 'this is a description',
339+
additionalProperties: {}
340+
}
341+
}
342+
}
343+
}
344+
}
345+
}
346+
}
347+
}
348+
};
349+
const res = validate({ jsSpec: spec, isOAS3: true }, config);
350+
expect(res.warnings.length).toEqual(0);
351+
expect(res.errors.length).toEqual(0);
352+
});
353+
305354
it('should return an error when a parameter defines content-type, accept, or authorization', () => {
306355
const spec = {
307356
paths: {
@@ -510,5 +559,31 @@ describe('validation plugin - semantic - parameters-ibm', () => {
510559
expect(res.warnings.length).toEqual(0);
511560
expect(res.errors.length).toEqual(0);
512561
});
562+
563+
it('should complain about parameters not defined properly in a path item ', () => {
564+
const spec = {
565+
paths: {
566+
'/pets': {
567+
parameters: [
568+
{
569+
name: 'tags',
570+
in: 'query',
571+
schema: {
572+
type: 'string',
573+
format: 'binary'
574+
}
575+
}
576+
]
577+
}
578+
}
579+
};
580+
581+
const res = validate({ jsSpec: spec, isOAS3: true }, config);
582+
expect(res.warnings.length).toEqual(0);
583+
expect(res.errors.length).toEqual(1);
584+
expect(res.errors[0].message).toEqual(
585+
'Parameter objects must have a `description` field.'
586+
);
587+
});
513588
});
514589
});

0 commit comments

Comments
 (0)