feat: Color of "default" Response depends on other successful responses are specified

closes #197
This commit is contained in:
Roman Hotsiy 2017-02-26 00:37:57 +02:00
parent d175a4d6ae
commit 9d0dd25f30
No known key found for this signature in database
GPG Key ID: 5CB7B3ACABA57CB0
5 changed files with 45 additions and 6 deletions

View File

@ -53,5 +53,12 @@ describe('Redoc components', () => {
let resp2 = component.responses[1];
resp1.code.should.not.be.equal(resp2.code);
});
it('should set type of default as error if other 200-399 response is defined', () => {
component.pointer = '#/paths/~1test2/get/responses';
fixture.detectChanges();
let resp1 = component.responses[1];
resp1.type.should.be.equal('error');
});
});
});

View File

@ -44,10 +44,16 @@ export class ResponsesList extends BaseSearchableComponent implements OnInit {
let responses = this.componentSchema;
if (!responses) return;
responses = Object.keys(responses).filter(respCode => {
let hasSuccessResponses = false;
let respCodes = Object.keys(responses).filter(respCode => {
if ((parseInt(respCode) >= 100) && (parseInt(respCode) <=399)) {
hasSuccessResponses = true;
}
// only response-codes and "default"
return ( isNumeric(respCode) || (respCode === 'default'));
}).map(respCode => {
});
responses = respCodes.map(respCode => {
let resp = responses[respCode];
resp.pointer = JsonPointer.join(this.pointer, respCode);
if (resp.$ref) {
@ -58,7 +64,7 @@ export class ResponsesList extends BaseSearchableComponent implements OnInit {
resp.empty = !resp.schema;
resp.code = respCode;
resp.type = statusCodeType(resp.code);
resp.type = statusCodeType(resp.code, hasSuccessResponses);
resp.expanded = false;
if (this.options.expandResponses) {

View File

@ -37,7 +37,11 @@ export class ResponsesSamples extends BaseComponent implements OnInit {
let responses = this.componentSchema;
if (!responses) return;
let hasSuccessResponses = false;
responses = Object.keys(responses).filter(respCode => {
if ((parseInt(respCode) >= 100) && (parseInt(respCode) <=399)) {
hasSuccessResponses = true;
}
// only response-codes and "default"
return ( isNumeric(respCode) || (respCode === 'default'));
}).map(respCode => {
@ -50,7 +54,7 @@ export class ResponsesSamples extends BaseComponent implements OnInit {
}
resp.code = respCode;
resp.type = statusCodeType(resp.code);
resp.type = statusCodeType(resp.code, hasSuccessResponses);
return resp;
})
.filter(response => hasExample(response));

View File

@ -32,7 +32,11 @@ export function groupBy<T>(array: T[], key:string):StringMap<T[]> {
}, {});
}
export function statusCodeType(statusCode) {
export function statusCodeType(statusCode, defaultAsError = false) {
if (statusCode === 'default') {
return defaultAsError ? 'error' : 'success';
}
if (statusCode < 100 || statusCode > 599) {
throw new Error('invalid HTTP code');
}

View File

@ -31,7 +31,25 @@
}
}
}
}
},
"/test2": {
"get": {
"responses": {
"200": {
"description": "successful operation",
"schema": {
"$ref": "#/definitions/def1"
}
},
"default": {
"description": "successful operation",
"schema": {
"$ref": "#/definitions/def1"
}
}
}
}
},
},
"definitions": {
"def1": {