Skip to content

Commit 7b1a9d3

Browse files
authored
fix: flag any operations with array responses (#73)
only get operations were being flagged before, which was incorrect behavior
1 parent aed7514 commit 7b1a9d3

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/plugins/validation/2and3/semantic-validators/operations-shared.js

+21-24
Original file line numberDiff line numberDiff line change
@@ -67,32 +67,29 @@ module.exports.validate = function({ resolvedSpec, isOAS3 }, config) {
6767
});
6868

6969
// Arrays MUST NOT be returned as the top-level structure in a response body.
70-
const isGetOperation = opKey.toLowerCase() === 'get';
71-
if (isGetOperation) {
72-
const checkStatus = config.no_array_responses;
73-
if (checkStatus !== 'off') {
74-
each(op.responses, (response, name) => {
75-
if (isOAS3) {
76-
each(response.content, (content, contentType) => {
77-
if (content.schema && content.schema.type === 'array') {
78-
result[checkStatus].push({
79-
path: `paths.${pathKey}.${opKey}.responses.${name}.content.${contentType}.schema`,
80-
message:
81-
'Arrays MUST NOT be returned as the top-level structure in a response body.'
82-
});
83-
}
84-
});
85-
} else {
86-
if (response.schema && response.schema.type === 'array') {
87-
result[checkStatus].push({
88-
path: `paths.${pathKey}.${opKey}.responses.${name}.schema`,
70+
const checkStatusArrRes = config.no_array_responses;
71+
if (checkStatusArrRes !== 'off') {
72+
each(op.responses, (response, name) => {
73+
if (isOAS3) {
74+
each(response.content, (content, contentType) => {
75+
if (content.schema && content.schema.type === 'array') {
76+
result[checkStatusArrRes].push({
77+
path: `paths.${pathKey}.${opKey}.responses.${name}.content.${contentType}.schema`,
8978
message:
9079
'Arrays MUST NOT be returned as the top-level structure in a response body.'
9180
});
9281
}
82+
});
83+
} else {
84+
if (response.schema && response.schema.type === 'array') {
85+
result[checkStatusArrRes].push({
86+
path: `paths.${pathKey}.${opKey}.responses.${name}.schema`,
87+
message:
88+
'Arrays MUST NOT be returned as the top-level structure in a response body.'
89+
});
9390
}
94-
});
95-
}
91+
}
92+
});
9693
}
9794

9895
const hasOperationId =
@@ -153,8 +150,8 @@ module.exports.validate = function({ resolvedSpec, isOAS3 }, config) {
153150

154151
// this should be good with resolved spec, but double check
155152
// All required parameters of an operation are listed before any optional parameters.
156-
const checkStatus = config.parameter_order;
157-
if (checkStatus !== 'off') {
153+
const checkStatusParamOrder = config.parameter_order;
154+
if (checkStatusParamOrder !== 'off') {
158155
if (op.parameters && op.parameters.length > 0) {
159156
let firstOptional = -1;
160157
for (let indx = 0; indx < op.parameters.length; indx++) {
@@ -165,7 +162,7 @@ module.exports.validate = function({ resolvedSpec, isOAS3 }, config) {
165162
}
166163
} else {
167164
if (param.required) {
168-
result[checkStatus].push({
165+
result[checkStatusParamOrder].push({
169166
path: `paths.${pathKey}.${opKey}.parameters[${indx}]`,
170167
message:
171168
'Required parameters should appear before optional parameters.'

test/plugins/validation/2and3/operations-shared.js

+14-4
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ describe('validation plugin - semantic - operations-shared', function() {
255255
const spec = {
256256
paths: {
257257
'/stuff': {
258-
get: {
258+
post: {
259259
summary: 'list stuff',
260260
operationId: 'list_stuff',
261261
produces: ['application/json'],
@@ -285,7 +285,7 @@ describe('validation plugin - semantic - operations-shared', function() {
285285
const res = validate({ resolvedSpec }, config);
286286
expect(res.errors.length).toEqual(1);
287287
expect(res.errors[0].path).toEqual(
288-
'paths./stuff.get.responses.200.schema'
288+
'paths./stuff.post.responses.200.schema'
289289
);
290290
expect(res.errors[0].message).toEqual(
291291
'Arrays MUST NOT be returned as the top-level structure in a response body.'
@@ -678,9 +678,19 @@ describe('validation plugin - semantic - operations-shared', function() {
678678
const spec = {
679679
paths: {
680680
'/': {
681-
get: {
681+
put: {
682682
operationId: 'get_everything',
683683
summary: 'get everything as a string or an array',
684+
requestBody: {
685+
description: 'simple body',
686+
content: {
687+
'application/json': {
688+
schema: {
689+
type: 'string'
690+
}
691+
}
692+
}
693+
},
684694
responses: {
685695
'200': {
686696
content: {
@@ -708,7 +718,7 @@ describe('validation plugin - semantic - operations-shared', function() {
708718
const res = validate({ resolvedSpec: spec, isOAS3: true }, config);
709719
expect(res.errors.length).toEqual(1);
710720
expect(res.errors[0].path).toEqual(
711-
'paths./.get.responses.200.content.application/json.schema'
721+
'paths./.put.responses.200.content.application/json.schema'
712722
);
713723
expect(res.errors[0].message).toEqual(
714724
'Arrays MUST NOT be returned as the top-level structure in a response body.'

0 commit comments

Comments
 (0)