From f82eb53ccf7615f493a5156fa24d7f80ce8d94db Mon Sep 17 00:00:00 2001 From: Shelby Sanders Date: Thu, 9 Dec 2021 09:43:47 -0800 Subject: [PATCH] Added support for showExtensions and $ref for Responses (#1406) * Corrected responses to support $ref * Add showExtentions for Responses * Add showExtentions support for Responses * Update src/services/__tests__/models/Response.test.ts Co-authored-by: Giles Wells Co-authored-by: Roman Hotsiy Co-authored-by: Giles Wells --- src/components/Responses/Response.tsx | 5 +++-- src/components/Responses/ResponseDetails.tsx | 4 +++- src/services/__tests__/models/Response.test.ts | 7 +++++++ src/services/models/Response.ts | 10 +++++++++- src/types/open-api.ts | 2 +- 5 files changed, 23 insertions(+), 5 deletions(-) diff --git a/src/components/Responses/Response.tsx b/src/components/Responses/Response.tsx index 58e0ae32e2..bc6ece8bd4 100644 --- a/src/components/Responses/Response.tsx +++ b/src/components/Responses/Response.tsx @@ -12,11 +12,12 @@ export class ResponseView extends React.Component<{ response: ResponseModel }> { }; render() { - const { headers, type, summary, description, code, expanded, content } = this.props.response; + const { extensions, headers, type, summary, description, code, expanded, content } = this.props.response; const mimes = content === undefined ? [] : content.mediaTypes.filter(mime => mime.schema !== undefined); - const empty = headers.length === 0 && mimes.length === 0 && !description; + const empty = (!extensions || Object.keys(extensions).length === 0) && + headers.length === 0 && mimes.length === 0 && !description; return (
diff --git a/src/components/Responses/ResponseDetails.tsx b/src/components/Responses/ResponseDetails.tsx index 821fc2da99..3794eba1da 100644 --- a/src/components/Responses/ResponseDetails.tsx +++ b/src/components/Responses/ResponseDetails.tsx @@ -7,15 +7,17 @@ import { DropdownOrLabel } from '../DropdownOrLabel/DropdownOrLabel'; import { MediaTypesSwitch } from '../MediaTypeSwitch/MediaTypesSwitch'; import { Schema } from '../Schema'; +import { Extensions } from '../Fields/Extensions'; import { Markdown } from '../Markdown/Markdown'; import { ResponseHeaders } from './ResponseHeaders'; export class ResponseDetails extends React.PureComponent<{ response: ResponseModel }> { render() { - const { description, headers, content } = this.props.response; + const { description, extensions, headers, content } = this.props.response; return ( <> {description && } + {({ schema }) => { diff --git a/src/services/__tests__/models/Response.test.ts b/src/services/__tests__/models/Response.test.ts index ebb836c03a..4863361954 100644 --- a/src/services/__tests__/models/Response.test.ts +++ b/src/services/__tests__/models/Response.test.ts @@ -39,5 +39,12 @@ describe('Models', () => { const resp = new ResponseModel({ ...props, code: 'default', defaultAsError: true }); expect(resp.type).toEqual('error'); }); + + test('ensure extensions are shown if showExtensions is true', () => { + const options = new RedocNormalizedOptions({ showExtensions: true }); + const resp = new ResponseModel(parser, 'default', true, { 'x-example': {a: 1} } as any, options); + expect(Object.keys(resp.extensions).length).toEqual(1); + expect(resp.extensions['x-example']).toEqual({a: 1}); + }); }); }); diff --git a/src/services/models/Response.ts b/src/services/models/Response.ts index 0a26413291..a0878093ed 100644 --- a/src/services/models/Response.ts +++ b/src/services/models/Response.ts @@ -2,7 +2,10 @@ import { action, observable, makeObservable } from 'mobx'; import { OpenAPIResponse, Referenced } from '../../types'; -import { getStatusCodeType } from '../../utils'; +import { + getStatusCodeType, + extractExtensions +} from '../../utils'; import { OpenAPIParser } from '../OpenAPIParser'; import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; import { FieldModel } from './Field'; @@ -27,6 +30,7 @@ export class ResponseModel { description: string; type: string; headers: FieldModel[] = []; + extensions: Record; constructor(props: ResponseProps) { const { parser, code, defaultAsError, infoOrRef, options, isEvent } = props; @@ -59,6 +63,10 @@ export class ResponseModel { return new FieldModel(parser, { ...header, name }, '', options); }); } + + if (options.showExtensions) { + this.extensions = extractExtensions(info, options.showExtensions); + } } @action diff --git a/src/types/open-api.ts b/src/types/open-api.ts index 622c5163e7..7038aa7ab6 100644 --- a/src/types/open-api.ts +++ b/src/types/open-api.ts @@ -192,7 +192,7 @@ export interface OpenAPIRequestBody { } export interface OpenAPIResponses { - [code: string]: OpenAPIResponse; + [code: string]: Referenced; } export interface OpenAPIResponse