Add showExtentions support for Responses

This commit is contained in:
Shelby Sanders 2020-10-08 18:39:29 -07:00
parent 918e2414f6
commit 69f1974a6c
4 changed files with 23 additions and 4 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

@ -31,5 +31,12 @@ describe('Models', () => {
const resp = new ResponseModel(parser, 'default', true, {}, opts);
expect(resp.type).toEqual('error');
});
test('should be error 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 } from 'mobx';
import { OpenAPIResponse, Referenced } from '../../types';
import { getStatusCodeType } from '../../utils';
import {
extractExtensions,
getStatusCodeType,
} from '../../utils';
import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
import { FieldModel } from './Field';
@ -19,6 +22,8 @@ export class ResponseModel {
type: string;
headers: FieldModel[] = [];
extensions: Record<string, any>;
constructor(
parser: OpenAPIParser,
code: string,
@ -52,6 +57,10 @@ export class ResponseModel {
return new FieldModel(parser, { ...header, name }, '', options);
});
}
if (options.showExtensions) {
this.extensions = extractExtensions(info, options.showExtensions);
}
}
@action