Default response should be error if there is sucessfull response

This commit is contained in:
Roman Hotsiy 2017-11-14 12:53:56 +02:00
parent 595d5e02a9
commit 162ad11497
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
4 changed files with 47 additions and 6 deletions

View File

@ -0,0 +1,35 @@
import { ResponseModel } from '../../models/Response';
import { OpenAPIParser } from '../../OpenAPIParser';
describe('Models', () => {
describe('ResponseModel', () => {
let parser;
beforeEach(() => {
parser = new OpenAPIParser();
parser.spec = <any>{};
})
test('should calculate response type based on code', () => {
let resp = new ResponseModel(parser, '200', false, {});
expect(resp.type).toEqual('success');
resp = new ResponseModel(parser, '120', false, {});
expect(resp.type).toEqual('info');
resp = new ResponseModel(parser, '301', false, {});
expect(resp.type).toEqual('redirect');
resp = new ResponseModel(parser, '400', false, {});
expect(resp.type).toEqual('error');
});
test('default should be sucessful by default', () => {
let resp = new ResponseModel(parser, 'default', false, {});
expect(resp.type).toEqual('success');
});
test('default should be error if defaultAsError is true', () => {
let resp = new ResponseModel(parser, 'default', true, {});
expect(resp.type).toEqual('error');
});
});
});

View File

@ -73,9 +73,15 @@ export class OperationModel implements IMenuItem {
paramOrRef => new FieldModel(parser, paramOrRef, this._$ref), paramOrRef => new FieldModel(parser, paramOrRef, this._$ref),
); );
let hasSuccessResponses = false;
this.responses = Object.keys(operationSpec.responses || []) this.responses = Object.keys(operationSpec.responses || [])
.filter(code => isNumeric(code) || code === 'default') // filter out other props (e.g. x-props) .filter(code => {
.map(code => new ResponseModel(parser, code, operationSpec.responses[code])); if (parseInt(code) >= 100 && parseInt(code) <= 399) {
hasSuccessResponses = true;
}
return isNumeric(code) || code === 'default'
}) // filter out other props (e.g. x-props)
.map(code => new ResponseModel(parser, code, hasSuccessResponses, operationSpec.responses[code]));
this.servers = normalizeServers( this.servers = normalizeServers(
parser.specUrl, parser.specUrl,

View File

@ -16,7 +16,7 @@ export class ResponseModel {
public type: string; public type: string;
public headers: FieldModel[] = []; public headers: FieldModel[] = [];
constructor(parser: OpenAPIParser, code: string, infoOrRef: Referenced<OpenAPIResponse>) { constructor(parser: OpenAPIParser, code: string, defaultAsError: boolean, infoOrRef: Referenced<OpenAPIResponse>) {
const info = parser.deref(infoOrRef); const info = parser.deref(infoOrRef);
parser.exitRef(infoOrRef); parser.exitRef(infoOrRef);
this.code = code; this.code = code;
@ -24,7 +24,7 @@ export class ResponseModel {
this.content = new MediaContentModel(parser, info.content, false); this.content = new MediaContentModel(parser, info.content, false);
} }
this.description = info.description || ''; this.description = info.description || '';
this.type = getStatusCodeType(code); this.type = getStatusCodeType(code, defaultAsError);
const headers = info.headers; const headers = info.headers;
if (headers !== undefined) { if (headers !== undefined) {

View File

@ -178,8 +178,8 @@ export type OpenAPIResponses = {
export type OpenAPIResponse = { export type OpenAPIResponse = {
description?: string; description?: string;
headers?: { [name: string]: Referenced<OpenAPIHeader> }; headers?: { [name: string]: Referenced<OpenAPIHeader> };
content: { [mime: string]: OpenAPIMediaType }; content?: { [mime: string]: OpenAPIMediaType };
links: { [name: string]: Referenced<OpenAPILink> }; links?: { [name: string]: Referenced<OpenAPILink> };
}; };
export type OpenAPILink = {}; export type OpenAPILink = {};