diff --git a/src/plugins/validation/oas3/semantic-validators/responses.js b/src/plugins/validation/oas3/semantic-validators/responses.js index bccb4b82d..2a574dae8 100644 --- a/src/plugins/validation/oas3/semantic-validators/responses.js +++ b/src/plugins/validation/oas3/semantic-validators/responses.js @@ -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']), diff --git a/test/plugins/validation/oas3/responses.js b/test/plugins/validation/oas3/responses.js index 25f161f8a..b0ae8cc0c 100644 --- a/test/plugins/validation/oas3/responses.js +++ b/test/plugins/validation/oas3/responses.js @@ -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'); @@ -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( @@ -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( @@ -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); }); @@ -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', @@ -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', @@ -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: { @@ -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', @@ -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([