Skip to content

Commit

Permalink
feat: Color of "default" Response depends on other successful respons…
Browse files Browse the repository at this point in the history
…es are specified

closes #197
  • Loading branch information
RomanHotsiy committed Feb 25, 2017
1 parent d175a4d commit 9d0dd25
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
7 changes: 7 additions & 0 deletions lib/components/ResponsesList/responses-list.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
12 changes: 9 additions & 3 deletions lib/components/ResponsesList/responses-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion lib/components/ResponsesSamples/responses-samples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand All @@ -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));
Expand Down
6 changes: 5 additions & 1 deletion lib/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
20 changes: 19 additions & 1 deletion tests/schemas/responses-list-component.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down

0 comments on commit 9d0dd25

Please sign in to comment.