Skip to content

Commit 1d97cb2

Browse files
fix: swagger2 validator should only report error if there's body parameter but no consume (#123)
* removed check for consume in put or post operation * added test cases to check no error produced when there's no body param and no consume
1 parent c6b5510 commit 1d97cb2

File tree

2 files changed

+141
-7
lines changed

2 files changed

+141
-7
lines changed

src/plugins/validation/swagger2/semantic-validators/operations-ibm.js

+27-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Assertation 1:
2-
// PUT and POST operations must have a non-empty `consumes` field
2+
// PUT and POST operations with body parameter must have a non-empty `consumes` field
33

44
// Assertation 2:
55
// GET operations should not specify a consumes field.
@@ -46,14 +46,38 @@ module.exports.validate = function({ jsSpec }, config) {
4646
!!op.consumes.join('').trim();
4747
const hasGlobalConsumes = !!jsSpec.consumes;
4848

49-
if (!hasLocalConsumes && !hasGlobalConsumes) {
49+
// Check for body parameter in path
50+
let hasBodyParamInPath = false;
51+
if (path.parameters) {
52+
path.parameters.forEach(parameter => {
53+
if (parameter.in === 'body') {
54+
hasBodyParamInPath = true;
55+
}
56+
});
57+
}
58+
59+
// Check for body parameter in operation
60+
let hasBodyParamInOps = false;
61+
if (op.parameters) {
62+
op.parameters.forEach(parameter => {
63+
if (parameter.in === 'body') {
64+
hasBodyParamInOps = true;
65+
}
66+
});
67+
}
68+
69+
if (
70+
!hasLocalConsumes &&
71+
!hasGlobalConsumes &&
72+
(hasBodyParamInOps || hasBodyParamInPath)
73+
) {
5074
const checkStatus = config.no_consumes_for_put_or_post;
5175

5276
if (checkStatus !== 'off') {
5377
result[checkStatus].push({
5478
path: `paths.${pathKey}.${opKey}.consumes`,
5579
message:
56-
'PUT and POST operations must have a non-empty `consumes` field.'
80+
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
5781
});
5882
}
5983
}

test/plugins/validation/swagger2/operations-ibm.js

+114-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const {
44
} = require('../../../../src/plugins/validation/swagger2/semantic-validators/operations-ibm');
55

66
describe('validation plugin - semantic - operations-ibm - swagger2', function() {
7-
it('should complain about a missing consumes with content', function() {
7+
it('should complain about PUT operation with body parameter and a missing consumes', function() {
88
const config = {
99
operations: {
1010
no_consumes_for_put_or_post: 'error'
@@ -41,12 +41,12 @@ describe('validation plugin - semantic - operations-ibm - swagger2', function()
4141
expect(res.errors.length).toEqual(1);
4242
expect(res.errors[0].path).toEqual('paths./CoolPath.put.consumes');
4343
expect(res.errors[0].message).toEqual(
44-
'PUT and POST operations must have a non-empty `consumes` field.'
44+
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
4545
);
4646
expect(res.warnings.length).toEqual(0);
4747
});
4848

49-
it('should complain about an empty consumes', function() {
49+
it('should complain about POST operation with body parameter and a missing consumes', function() {
5050
const config = {
5151
operations: {
5252
no_consumes_for_put_or_post: 'error'
@@ -84,11 +84,121 @@ describe('validation plugin - semantic - operations-ibm - swagger2', function()
8484
expect(res.errors.length).toEqual(1);
8585
expect(res.errors[0].path).toEqual('paths./CoolPath.post.consumes');
8686
expect(res.errors[0].message).toEqual(
87-
'PUT and POST operations must have a non-empty `consumes` field.'
87+
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
8888
);
8989
expect(res.warnings.length).toEqual(0);
9090
});
9191

92+
it('should complain about PUT opeartion with body parameter in path and a missing consumes', function() {
93+
const config = {
94+
operations: {
95+
no_consumes_for_put_or_post: 'error'
96+
}
97+
};
98+
99+
const spec = {
100+
paths: {
101+
'/CoolPath': {
102+
parameters: [
103+
{
104+
name: 'BadParameter',
105+
in: 'body',
106+
schema: {
107+
required: ['Property'],
108+
properties: [
109+
{
110+
name: 'Property'
111+
}
112+
]
113+
}
114+
}
115+
],
116+
put: {
117+
consumes: [' '],
118+
produces: ['application/json'],
119+
summary: 'this is a summary',
120+
operationId: 'operationId'
121+
}
122+
}
123+
}
124+
};
125+
126+
const res = validate({ jsSpec: spec }, config);
127+
expect(res.errors.length).toEqual(1);
128+
expect(res.errors[0].path).toEqual('paths./CoolPath.put.consumes');
129+
expect(res.errors[0].message).toEqual(
130+
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
131+
);
132+
expect(res.warnings.length).toEqual(0);
133+
});
134+
135+
it('should complain about POST opeartion with body parameter in path and a missing consumes', function() {
136+
const config = {
137+
operations: {
138+
no_consumes_for_put_or_post: 'error'
139+
}
140+
};
141+
142+
const spec = {
143+
paths: {
144+
'/CoolPath': {
145+
parameters: [
146+
{
147+
name: 'BadParameter',
148+
in: 'body',
149+
schema: {
150+
required: ['Property'],
151+
properties: [
152+
{
153+
name: 'Property'
154+
}
155+
]
156+
}
157+
}
158+
],
159+
post: {
160+
consumes: [' '],
161+
produces: ['application/json'],
162+
summary: 'this is a summary',
163+
operationId: 'operationId'
164+
}
165+
}
166+
}
167+
};
168+
169+
const res = validate({ jsSpec: spec }, config);
170+
expect(res.errors.length).toEqual(1);
171+
expect(res.errors[0].path).toEqual('paths./CoolPath.post.consumes');
172+
expect(res.errors[0].message).toEqual(
173+
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
174+
);
175+
expect(res.warnings.length).toEqual(0);
176+
});
177+
178+
it('should not complain about missing consumes when there is no body parameter', function() {
179+
const config = {
180+
operations: {
181+
no_consumes_for_put_or_post: 'error'
182+
}
183+
};
184+
185+
const spec = {
186+
paths: {
187+
'/CoolPath': {
188+
put: {
189+
produces: ['application/json'],
190+
summary: 'this is a summary',
191+
operationId: 'operationId'
192+
}
193+
}
194+
}
195+
};
196+
197+
const res = validate({ jsSpec: spec }, config);
198+
expect(res.errors.length).toEqual(0);
199+
expect(res.warnings.length).toEqual(0);
200+
});
201+
92202
it('should not complain about a missing consumes when there is a global consumes', function() {
93203
const config = {
94204
operations: {

0 commit comments

Comments
 (0)