Skip to content

Commit

Permalink
fix: add validation for use of 101 status code (#237)
Browse files Browse the repository at this point in the history
* Add validation for use of 101 status code

* Fix lint errors

* Rearrange checks to make more checks run for a response with a 101 and 2xx response
  • Loading branch information
hudlow committed Feb 18, 2021
1 parent adbe6bc commit 52e1319
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/.defaultsForValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ const defaults = {
'responses': {
'no_response_codes': 'error',
'no_success_response_codes': 'warning',
'protocol_switching_and_success_code': 'error',
'no_response_body': 'warning',
'ibm_status_code_guidelines': 'warning'
},
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/validation/oas3/semantic-validators/responses.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,22 @@ module.exports.validate = function({ resolvedSpec }, config) {
}
}
// validate all success codes
if (!successCodes.length) {
if (!successCodes.length && !('101' in obj)) {
messages.addMessage(
path,
'Each `responses` object SHOULD have at least one code for a successful response.',
config.no_success_response_codes,
'no_success_response_codes'
);
} else {
if (successCodes.length && '101' in obj) {
messages.addMessage(
path,
'A `responses` object MUST NOT support 101 and any success (2xx) code.',
config.protocol_switching_and_success_code,
'protocol_switching_and_success_code'
);
}
for (const statusCode of successCodes) {
if (statusCode !== '204' && !obj[statusCode].content) {
messages.addMessage(
Expand Down
52 changes: 52 additions & 0 deletions test/plugins/validation/oas3/responses.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,58 @@ describe('validation plugin - semantic - responses - oas3', function() {
expect(res.errors.length).toEqual(0);
});

it('should not complain about having only a 101 response', function() {
const spec = {
paths: {
'/pets': {
get: {
summary: 'this is a summary',
operationId: 'operationId',
responses: {
'101': {
description: 'switching protocols'
}
}
}
}
}
};

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

it('should complain about having a 101 along with any 2xx code', function() {
const spec = {
paths: {
'/pets': {
get: {
summary: 'this is a summary',
operationId: 'operationId',
responses: {
'101': {
description: 'switching protocols'
},
'204': {
description: 'no content'
}
}
}
}
}
};

const res = validate({ resolvedSpec: spec }, config);
expect(res.warnings.length).toEqual(0);
expect(res.errors.length).toEqual(1);
expect(res.errors[0].path).toEqual(['paths', '/pets', 'get', 'responses']);
expect(res.errors[0].message).toEqual(
'A `responses` object MUST NOT support 101 and any success (2xx) code.'
);
});

it('should complain about 204 response that defines a response body', function() {
const spec = {
paths: {
Expand Down

0 comments on commit 52e1319

Please sign in to comment.