Skip to content

Commit

Permalink
fix: swagger2 validator should only report error if there's body para…
Browse files Browse the repository at this point in the history
…meter 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
  • Loading branch information
SaltedCaramelCoffee authored Jan 27, 2020
1 parent c6b5510 commit 1d97cb2
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Assertation 1:
// PUT and POST operations must have a non-empty `consumes` field
// PUT and POST operations with body parameter must have a non-empty `consumes` field

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

if (!hasLocalConsumes && !hasGlobalConsumes) {
// Check for body parameter in path
let hasBodyParamInPath = false;
if (path.parameters) {
path.parameters.forEach(parameter => {
if (parameter.in === 'body') {
hasBodyParamInPath = true;
}
});
}

// Check for body parameter in operation
let hasBodyParamInOps = false;
if (op.parameters) {
op.parameters.forEach(parameter => {
if (parameter.in === 'body') {
hasBodyParamInOps = true;
}
});
}

if (
!hasLocalConsumes &&
!hasGlobalConsumes &&
(hasBodyParamInOps || hasBodyParamInPath)
) {
const checkStatus = config.no_consumes_for_put_or_post;

if (checkStatus !== 'off') {
result[checkStatus].push({
path: `paths.${pathKey}.${opKey}.consumes`,
message:
'PUT and POST operations must have a non-empty `consumes` field.'
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
});
}
}
Expand Down
118 changes: 114 additions & 4 deletions test/plugins/validation/swagger2/operations-ibm.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
} = require('../../../../src/plugins/validation/swagger2/semantic-validators/operations-ibm');

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

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

it('should complain about PUT opeartion with body parameter in path and a missing consumes', function() {
const config = {
operations: {
no_consumes_for_put_or_post: 'error'
}
};

const spec = {
paths: {
'/CoolPath': {
parameters: [
{
name: 'BadParameter',
in: 'body',
schema: {
required: ['Property'],
properties: [
{
name: 'Property'
}
]
}
}
],
put: {
consumes: [' '],
produces: ['application/json'],
summary: 'this is a summary',
operationId: 'operationId'
}
}
}
};

const res = validate({ jsSpec: spec }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual('paths./CoolPath.put.consumes');
expect(res.errors[0].message).toEqual(
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
);
expect(res.warnings.length).toEqual(0);
});

it('should complain about POST opeartion with body parameter in path and a missing consumes', function() {
const config = {
operations: {
no_consumes_for_put_or_post: 'error'
}
};

const spec = {
paths: {
'/CoolPath': {
parameters: [
{
name: 'BadParameter',
in: 'body',
schema: {
required: ['Property'],
properties: [
{
name: 'Property'
}
]
}
}
],
post: {
consumes: [' '],
produces: ['application/json'],
summary: 'this is a summary',
operationId: 'operationId'
}
}
}
};

const res = validate({ jsSpec: spec }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual('paths./CoolPath.post.consumes');
expect(res.errors[0].message).toEqual(
'PUT and POST operations with a body parameter must have a non-empty `consumes` field.'
);
expect(res.warnings.length).toEqual(0);
});

it('should not complain about missing consumes when there is no body parameter', function() {
const config = {
operations: {
no_consumes_for_put_or_post: 'error'
}
};

const spec = {
paths: {
'/CoolPath': {
put: {
produces: ['application/json'],
summary: 'this is a summary',
operationId: 'operationId'
}
}
}
};

const res = validate({ jsSpec: spec }, config);
expect(res.errors.length).toEqual(0);
expect(res.warnings.length).toEqual(0);
});

it('should not complain about a missing consumes when there is a global consumes', function() {
const config = {
operations: {
Expand Down

0 comments on commit 1d97cb2

Please sign in to comment.