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 <giles@mediagearhead.com>

Co-authored-by: Roman Hotsiy <gotsijroman@gmail.com>
Co-authored-by: Giles Wells <giles@mediagearhead.com>
This commit is contained in:
Shelby Sanders 2021-12-09 09:43:47 -08:00 committed by GitHub
parent af138639ca
commit f82eb53ccf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 23 additions and 5 deletions

View File

@ -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 (
<div>

View File

@ -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 && <Markdown source={description} />}
<Extensions extensions={extensions} />
<ResponseHeaders headers={headers} />
<MediaTypesSwitch content={content} renderDropdown={this.renderDropdown}>
{({ schema }) => {

View File

@ -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});
});
});
});

View File

@ -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<string, any>;
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

View File

@ -192,7 +192,7 @@ export interface OpenAPIRequestBody {
}
export interface OpenAPIResponses {
[code: string]: OpenAPIResponse;
[code: string]: Referenced<OpenAPIResponse>;
}
export interface OpenAPIResponse