mirror of
https://github.com/Redocly/redoc.git
synced 2024-11-26 02:23:43 +03:00
Default response should be error if there is sucessfull response
This commit is contained in:
parent
595d5e02a9
commit
162ad11497
35
src/services/__tests__/models/Response.test.ts
Normal file
35
src/services/__tests__/models/Response.test.ts
Normal 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');
|
||||
});
|
||||
});
|
||||
});
|
|
@ -73,9 +73,15 @@ export class OperationModel implements IMenuItem {
|
|||
paramOrRef => new FieldModel(parser, paramOrRef, this._$ref),
|
||||
);
|
||||
|
||||
let hasSuccessResponses = false;
|
||||
this.responses = Object.keys(operationSpec.responses || [])
|
||||
.filter(code => isNumeric(code) || code === 'default') // filter out other props (e.g. x-props)
|
||||
.map(code => new ResponseModel(parser, code, operationSpec.responses[code]));
|
||||
.filter(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(
|
||||
parser.specUrl,
|
||||
|
|
|
@ -16,7 +16,7 @@ export class ResponseModel {
|
|||
public type: string;
|
||||
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);
|
||||
parser.exitRef(infoOrRef);
|
||||
this.code = code;
|
||||
|
@ -24,7 +24,7 @@ export class ResponseModel {
|
|||
this.content = new MediaContentModel(parser, info.content, false);
|
||||
}
|
||||
this.description = info.description || '';
|
||||
this.type = getStatusCodeType(code);
|
||||
this.type = getStatusCodeType(code, defaultAsError);
|
||||
|
||||
const headers = info.headers;
|
||||
if (headers !== undefined) {
|
||||
|
|
|
@ -178,8 +178,8 @@ export type OpenAPIResponses = {
|
|||
export type OpenAPIResponse = {
|
||||
description?: string;
|
||||
headers?: { [name: string]: Referenced<OpenAPIHeader> };
|
||||
content: { [mime: string]: OpenAPIMediaType };
|
||||
links: { [name: string]: Referenced<OpenAPILink> };
|
||||
content?: { [mime: string]: OpenAPIMediaType };
|
||||
links?: { [name: string]: Referenced<OpenAPILink> };
|
||||
};
|
||||
|
||||
export type OpenAPILink = {};
|
||||
|
|
Loading…
Reference in New Issue
Block a user