From 9d0dd25f309634bf0c71ca0568150bd7c40c7975 Mon Sep 17 00:00:00 2001 From: Roman Hotsiy Date: Sun, 26 Feb 2017 00:37:57 +0200 Subject: [PATCH] feat: Color of "default" Response depends on other successful responses are specified closes #197 --- .../ResponsesList/responses-list.spec.ts | 7 +++++++ .../ResponsesList/responses-list.ts | 12 ++++++++--- .../ResponsesSamples/responses-samples.ts | 6 +++++- lib/utils/helpers.ts | 6 +++++- tests/schemas/responses-list-component.json | 20 ++++++++++++++++++- 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/lib/components/ResponsesList/responses-list.spec.ts b/lib/components/ResponsesList/responses-list.spec.ts index 9cec70f2..5cbf91cd 100644 --- a/lib/components/ResponsesList/responses-list.spec.ts +++ b/lib/components/ResponsesList/responses-list.spec.ts @@ -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'); + }); }); }); diff --git a/lib/components/ResponsesList/responses-list.ts b/lib/components/ResponsesList/responses-list.ts index 78d4966e..1c4315b0 100644 --- a/lib/components/ResponsesList/responses-list.ts +++ b/lib/components/ResponsesList/responses-list.ts @@ -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) { diff --git a/lib/components/ResponsesSamples/responses-samples.ts b/lib/components/ResponsesSamples/responses-samples.ts index 149c42f0..9e6a436b 100644 --- a/lib/components/ResponsesSamples/responses-samples.ts +++ b/lib/components/ResponsesSamples/responses-samples.ts @@ -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)); diff --git a/lib/utils/helpers.ts b/lib/utils/helpers.ts index 5ef344ea..86fcc839 100644 --- a/lib/utils/helpers.ts +++ b/lib/utils/helpers.ts @@ -32,7 +32,11 @@ export function groupBy(array: T[], key:string):StringMap { }, {}); } -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'); } diff --git a/tests/schemas/responses-list-component.json b/tests/schemas/responses-list-component.json index dccaa9d8..6f24a80f 100644 --- a/tests/schemas/responses-list-component.json +++ b/tests/schemas/responses-list-component.json @@ -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": {