Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: non-204 successes with reference to response #136

Merged
merged 1 commit into from
Feb 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/plugins/validation/oas3/semantic-validators/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,22 @@
// https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5

// Assertation 4:
// A non-204 success response MUST define a response body
// A non-204 success response should define a response body

const { walk } = require('../../../utils');

module.exports.validate = function({ jsSpec }, config) {
module.exports.validate = function({ resolvedSpec }, config) {
const result = {};
result.error = [];
result.warning = [];

config = config.responses;

walk(jsSpec, [], function(obj, path) {
walk(resolvedSpec, [], function(obj, path) {
const contentsOfResponsesObject =
path[0] === 'paths' && path[path.length - 1] === 'responses';
const isRef = !!obj.$ref;

if (contentsOfResponsesObject && !isRef) {
if (contentsOfResponsesObject) {
if (obj['204'] && obj['204'].content) {
result.error.push({
path: path.concat(['204', 'content']),
Expand Down
61 changes: 54 additions & 7 deletions test/plugins/validation/oas3/responses.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const expect = require('expect');
const resolver = require('json-schema-ref-parser');

const {
validate
} = require('../../../../src/plugins/validation/oas3/semantic-validators/responses');
Expand Down Expand Up @@ -29,7 +31,7 @@ describe('validation plugin - semantic - responses - oas3', function() {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ resolvedSpec: spec }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual(['paths', '/pets', 'get', 'responses']);
expect(res.errors[0].message).toEqual(
Expand Down Expand Up @@ -63,7 +65,7 @@ describe('validation plugin - semantic - responses - oas3', function() {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ resolvedSpec: spec }, config);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual(['paths', '/pets', 'get', 'responses']);
expect(res.errors[0].message).toEqual(
Expand Down Expand Up @@ -97,7 +99,7 @@ describe('validation plugin - semantic - responses - oas3', function() {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ resolvedSpec: spec }, config);
expect(res.errors.length).toEqual(0);
expect(res.warnings.length).toEqual(0);
});
Expand Down Expand Up @@ -127,7 +129,7 @@ describe('validation plugin - semantic - responses - oas3', function() {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ resolvedSpec: spec }, config);
expect(res.warnings.length).toEqual(1);
expect(res.warnings[0].path).toEqual([
'paths',
Expand Down Expand Up @@ -180,7 +182,7 @@ describe('validation plugin - semantic - responses - oas3', function() {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ resolvedSpec: spec }, config);
expect(res.warnings.length).toEqual(3);
expect(res.warnings[0].path).toEqual([
'paths',
Expand Down Expand Up @@ -214,6 +216,51 @@ describe('validation plugin - semantic - responses - oas3', function() {
);
});

it('should not complain when a non-204 success has a ref to a response with content', async function() {
const config = {
responses: {
no_response_codes: 'error',
no_success_response_codes: 'warning',
no_response_body: 'warning'
}
};

const resolvedSpec = {
paths: {
'/comments': {
post: {
operationId: 'add_comment',
summary: 'adds a comment',
responses: {
'201': {
$ref: '#/components/responses/success'
}
}
}
}
},
components: {
responses: {
success: {
description: 'successful post',
content: {
'application/json': {
schema: {
type: 'string'
}
}
}
}
}
}
};

const spec = await resolver.dereference(resolvedSpec);

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

it('should complain about having only error responses', function() {
const config = {
responses: {
Expand Down Expand Up @@ -248,7 +295,7 @@ describe('validation plugin - semantic - responses - oas3', function() {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ resolvedSpec: spec }, config);
expect(res.warnings.length).toEqual(1);
expect(res.warnings[0].path).toEqual([
'paths',
Expand Down Expand Up @@ -298,7 +345,7 @@ describe('validation plugin - semantic - responses - oas3', function() {
}
};

const res = validate({ jsSpec: spec }, config);
const res = validate({ resolvedSpec: spec }, config);
expect(res.warnings.length).toEqual(0);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual([
Expand Down