Skip to content

Commit

Permalink
fix: resolve refs when checking for success response schemas (#136)
Browse files Browse the repository at this point in the history
Use the resolvedSpec (all $refs dereferenced) to check if a response has content
  • Loading branch information
barrett-schonefeld authored Feb 11, 2020
1 parent c29fd43 commit b286c69
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 12 deletions.
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

0 comments on commit b286c69

Please sign in to comment.