`default` response is only applied for error status codes. (#64)

* `default` response is only applied for error status codes.

* 1.4.00
This commit is contained in:
Sergey Shandar 2018-12-06 17:01:33 -08:00 коммит произвёл GitHub
Родитель 0d632e770b
Коммит 20785fe256
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 50 добавлений и 19 удалений

Просмотреть файл

@ -190,9 +190,10 @@ Operation.prototype.getResponse = function (statusCode) {
statusCode = statusCode.toString();
}
return _.find(this.getResponses(), function (responseObject) {
return responseObject.statusCode === statusCode;
});
return _.find(
this.getResponses(),
responseObject => responseObject.statusCode === statusCode
);
};
/**
@ -244,8 +245,8 @@ Operation.prototype.validateRequest = function (req) {
warnings: []
};
// Validate the Content-Type if there is a set of expected consumes. Also we will validate only if there
// is a content-type set in the request headers as many times it happens that it is not set at all and
// Validate the Content-Type if there is a set of expected consumes. Also we will validate only if there
// is a content-type set in the request headers as many times it happens that it is not set at all and
// the service works fine. Going by the RFC www.w3.org/Protocols/rfc2616/rfc2616-sec7.html#sec7.2.1 mentioned above we would need to assume 'application/octet-stream'
// which in most cases would not be true so it would produce a lot of noise without adding much value.
if (this.consumes.length > 0 && helpers.getHeaderValue(req.headers, 'content-type')) {
@ -299,21 +300,21 @@ Operation.prototype.validateResponse = function (res) {
var response = this.getResponse(realStatusCode);
if (_.isUndefined(response)) {
const realCode = Number(realStatusCode)
// If there is no response for the requested status, use the default if there is one (This is Swagger's approach)
response = this.getResponse('default');
response = (400 <= realCode && realCode <= 599) ? this.getResponse('default') : undefined;
if (_.isUndefined(response)) {
results.errors.push({
code: 'INVALID_RESPONSE_CODE',
message: 'This operation does not have a defined \'' + (realStatusCode === 'default' ?
realStatusCode :
realStatusCode + '\' or \'default') + '\' response code',
message: 'This operation does not have a defined \'' + realStatusCode + '\' response code',
path: []
});
return results;
}
}
results = response.validateResponse(res);
return results;
};

Просмотреть файл

@ -1,6 +1,6 @@
{
"name": "yasway",
"version": "1.3.3",
"version": "1.4.0",
"description": "A library that simplifies Swagger integrations.",
"main": "index.js",
"types": "index.d.ts",

Просмотреть файл

@ -230,13 +230,13 @@ describe('Operation', function () {
};
describe('operation level consumes - ignore when empty', function () {
var operation;
var operation;
before(function () {
// this path+op doesn't specify 'consumes'
operation = swaggerApiRelativeRefs.getOperation('/pet/findByStatus', 'get');
});
it.skip('should not return an unsupported content-type error', function () {
var request = {
url: '/pet/findByStatus',
@ -487,19 +487,34 @@ describe('Operation', function () {
assert.deepEqual(results.errors, [
{
code: 'INVALID_RESPONSE_CODE',
message: 'This operation does not have a defined \'201\' or \'default\' response code',
message: 'This operation does not have a defined \'201\' response code',
path: []
}
]);
});
});
it('should return the \'default\' response when validating an undefined response', function () {
it('should return the \'default\' response when validating an undefined response from [400..600) range', function () {
var results = swaggerApiRelativeRefs.getOperation('/user', 'post').validateResponse({
statusCode: 599
});
assert.deepEqual(results.errors, []);
assert.deepEqual(results.warnings, []);
});
it('should return an error for an undefined non-error response, even if `default` is present', function () {
var results = swaggerApiRelativeRefs.getOperation('/user', 'post').validateResponse({
statusCode: 201
});
assert.deepEqual(results.errors, []);
assert.deepEqual(results.errors, [
{
code: 'INVALID_RESPONSE_CODE',
message: 'This operation does not have a defined \'201\' response code',
path: []
}
]);
assert.deepEqual(results.warnings, []);
});
});
@ -926,21 +941,36 @@ describe('Operation', function () {
assert.deepEqual(results.errors, [
{
code: 'INVALID_RESPONSE_CODE',
message: 'This operation does not have a defined \'201\' or \'default\' response code',
message: 'This operation does not have a defined \'201\' response code',
path: []
}
]);
});
});
it('should return the \'default\' response when validating an undefined response', function () {
it('should return the \'default\' response when validating an undefined response from 400-500 range', function () {
var results = swaggerApi.getOperation('/user', 'post').validateResponse({
statusCode: 201
statusCode: 404
});
assert.deepEqual(results.errors, []);
assert.deepEqual(results.warnings, []);
});
it('should return an error for an undefined non-error response, even if `default` is present', function () {
var results = swaggerApi.getOperation('/user', 'post').validateResponse({
statusCode: 201
});
assert.deepEqual(results.errors, [
{
code: 'INVALID_RESPONSE_CODE',
message: 'This operation does not have a defined \'201\' response code',
path: []
}
]);
assert.deepEqual(results.warnings, []);
});
});
});
});