Skip to content

Commit

Permalink
fix: flag any operations with array responses (#73)
Browse files Browse the repository at this point in the history
only get operations were being flagged before, which was incorrect behavior
  • Loading branch information
dpopp07 committed Jun 28, 2019
1 parent aed7514 commit 7b1a9d3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,32 +67,29 @@ module.exports.validate = function({ resolvedSpec, isOAS3 }, config) {
});

// Arrays MUST NOT be returned as the top-level structure in a response body.
const isGetOperation = opKey.toLowerCase() === 'get';
if (isGetOperation) {
const checkStatus = config.no_array_responses;
if (checkStatus !== 'off') {
each(op.responses, (response, name) => {
if (isOAS3) {
each(response.content, (content, contentType) => {
if (content.schema && content.schema.type === 'array') {
result[checkStatus].push({
path: `paths.${pathKey}.${opKey}.responses.${name}.content.${contentType}.schema`,
message:
'Arrays MUST NOT be returned as the top-level structure in a response body.'
});
}
});
} else {
if (response.schema && response.schema.type === 'array') {
result[checkStatus].push({
path: `paths.${pathKey}.${opKey}.responses.${name}.schema`,
const checkStatusArrRes = config.no_array_responses;
if (checkStatusArrRes !== 'off') {
each(op.responses, (response, name) => {
if (isOAS3) {
each(response.content, (content, contentType) => {
if (content.schema && content.schema.type === 'array') {
result[checkStatusArrRes].push({
path: `paths.${pathKey}.${opKey}.responses.${name}.content.${contentType}.schema`,
message:
'Arrays MUST NOT be returned as the top-level structure in a response body.'
});
}
});
} else {
if (response.schema && response.schema.type === 'array') {
result[checkStatusArrRes].push({
path: `paths.${pathKey}.${opKey}.responses.${name}.schema`,
message:
'Arrays MUST NOT be returned as the top-level structure in a response body.'
});
}
});
}
}
});
}

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

// this should be good with resolved spec, but double check
// All required parameters of an operation are listed before any optional parameters.
const checkStatus = config.parameter_order;
if (checkStatus !== 'off') {
const checkStatusParamOrder = config.parameter_order;
if (checkStatusParamOrder !== 'off') {
if (op.parameters && op.parameters.length > 0) {
let firstOptional = -1;
for (let indx = 0; indx < op.parameters.length; indx++) {
Expand All @@ -165,7 +162,7 @@ module.exports.validate = function({ resolvedSpec, isOAS3 }, config) {
}
} else {
if (param.required) {
result[checkStatus].push({
result[checkStatusParamOrder].push({
path: `paths.${pathKey}.${opKey}.parameters[${indx}]`,
message:
'Required parameters should appear before optional parameters.'
Expand Down
18 changes: 14 additions & 4 deletions test/plugins/validation/2and3/operations-shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ describe('validation plugin - semantic - operations-shared', function() {
const spec = {
paths: {
'/stuff': {
get: {
post: {
summary: 'list stuff',
operationId: 'list_stuff',
produces: ['application/json'],
Expand Down Expand Up @@ -285,7 +285,7 @@ describe('validation plugin - semantic - operations-shared', function() {
const res = validate({ resolvedSpec }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual(
'paths./stuff.get.responses.200.schema'
'paths./stuff.post.responses.200.schema'
);
expect(res.errors[0].message).toEqual(
'Arrays MUST NOT be returned as the top-level structure in a response body.'
Expand Down Expand Up @@ -678,9 +678,19 @@ describe('validation plugin - semantic - operations-shared', function() {
const spec = {
paths: {
'/': {
get: {
put: {
operationId: 'get_everything',
summary: 'get everything as a string or an array',
requestBody: {
description: 'simple body',
content: {
'application/json': {
schema: {
type: 'string'
}
}
}
},
responses: {
'200': {
content: {
Expand Down Expand Up @@ -708,7 +718,7 @@ describe('validation plugin - semantic - operations-shared', function() {
const res = validate({ resolvedSpec: spec, isOAS3: true }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual(
'paths./.get.responses.200.content.application/json.schema'
'paths./.put.responses.200.content.application/json.schema'
);
expect(res.errors[0].message).toEqual(
'Arrays MUST NOT be returned as the top-level structure in a response body.'
Expand Down

0 comments on commit 7b1a9d3

Please sign in to comment.