This commit is contained in:
Shelby Sanders 2021-06-02 18:17:30 +02:00 committed by GitHub
commit b12e981367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 114 additions and 21 deletions

8
e2e/expandSchemas.html Normal file
View File

@ -0,0 +1,8 @@
<html>
<body>
<redoc spec-url="../demo/openapi.yaml" expand-schemas="true"></redoc>
<script src="../bundles/redoc.standalone.js"></script>
</body>
</html>

View File

@ -0,0 +1,17 @@
describe('Schemas', () => {
it('expandSchemas != true', () => {
cy.visit('e2e/standalone.html');
cy.get('.api-content')
.find('.expanded')
.should('have.length', 0);
});
it('expandSchemas == true', () => {
cy.visit('e2e/expandSchemas.html');
cy.get('.api-content')
.find('.expanded')
.should('have.length', 146);
});
});

View File

@ -12,11 +12,12 @@ export class ResponseView extends React.Component<{ response: ResponseModel }> {
}; };
render() { 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 = const mimes =
content === undefined ? [] : content.mediaTypes.filter(mime => mime.schema !== undefined); 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 ( return (
<div> <div>

View File

@ -7,15 +7,17 @@ import { DropdownOrLabel } from '../DropdownOrLabel/DropdownOrLabel';
import { MediaTypesSwitch } from '../MediaTypeSwitch/MediaTypesSwitch'; import { MediaTypesSwitch } from '../MediaTypeSwitch/MediaTypesSwitch';
import { Schema } from '../Schema'; import { Schema } from '../Schema';
import { Extensions } from '../Fields/Extensions';
import { Markdown } from '../Markdown/Markdown'; import { Markdown } from '../Markdown/Markdown';
import { ResponseHeaders } from './ResponseHeaders'; import { ResponseHeaders } from './ResponseHeaders';
export class ResponseDetails extends React.PureComponent<{ response: ResponseModel }> { export class ResponseDetails extends React.PureComponent<{ response: ResponseModel }> {
render() { render() {
const { description, headers, content } = this.props.response; const { description, extensions, headers, content } = this.props.response;
return ( return (
<> <>
{description && <Markdown source={description} />} {description && <Markdown source={description} />}
<Extensions extensions={extensions} />
<ResponseHeaders headers={headers} /> <ResponseHeaders headers={headers} />
<MediaTypesSwitch content={content} renderDropdown={this.renderDropdown}> <MediaTypesSwitch content={content} renderDropdown={this.renderDropdown}>
{({ schema }) => { {({ schema }) => {

View File

@ -41,6 +41,7 @@ export interface RedocRawOptions {
expandDefaultServerVariables?: boolean; expandDefaultServerVariables?: boolean;
maxDisplayedEnumValues?: number; maxDisplayedEnumValues?: number;
ignoreNamedSchemas?: string[] | string; ignoreNamedSchemas?: string[] | string;
expandSchemas?: boolean;
hideSchemaPattern?: boolean; hideSchemaPattern?: boolean;
} }
@ -87,6 +88,10 @@ export class RedocNormalizedOptions {
return !!value; return !!value;
} }
static normalizeExpandSchemas(value: RedocRawOptions['expandSchemas']): boolean {
return !!value;
}
static normalizeScrollYOffset(value: RedocRawOptions['scrollYOffset']): () => number { static normalizeScrollYOffset(value: RedocRawOptions['scrollYOffset']): () => number {
// just number is not valid selector and leads to crash so checking if isNumeric here // just number is not valid selector and leads to crash so checking if isNumeric here
if (typeof value === 'string' && !isNumeric(value)) { if (typeof value === 'string' && !isNumeric(value)) {
@ -195,6 +200,7 @@ export class RedocNormalizedOptions {
maxDisplayedEnumValues?: number; maxDisplayedEnumValues?: number;
ignoreNamedSchemas: Set<string>; ignoreNamedSchemas: Set<string>;
expandSchemas: boolean;
hideSchemaPattern: boolean; hideSchemaPattern: boolean;
constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) { constructor(raw: RedocRawOptions, defaults: RedocRawOptions = {}) {
@ -256,6 +262,7 @@ export class RedocNormalizedOptions {
? raw.ignoreNamedSchemas ? raw.ignoreNamedSchemas
: raw.ignoreNamedSchemas?.split(',').map((s) => s.trim()); : raw.ignoreNamedSchemas?.split(',').map((s) => s.trim());
this.ignoreNamedSchemas = new Set(ignoreNamedSchemas); this.ignoreNamedSchemas = new Set(ignoreNamedSchemas);
this.expandSchemas = RedocNormalizedOptions.normalizeExpandSchemas(raw.expandSchemas);
this.hideSchemaPattern = argValueToBoolean(raw.hideSchemaPattern); this.hideSchemaPattern = argValueToBoolean(raw.hideSchemaPattern);
} }
} }

View File

@ -0,0 +1,20 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0",
"title": "Foo"
},
"components": {
"schemas": {
"Foo": {
"type": "object",
"properties": {
"foo": {
"type": "string"
}
},
"additionalProperties": true
}
}
}
}

View File

@ -31,5 +31,12 @@ describe('Models', () => {
const resp = new ResponseModel(parser, 'default', true, {}, opts); const resp = new ResponseModel(parser, 'default', true, {}, opts);
expect(resp.type).toEqual('error'); 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

@ -40,5 +40,25 @@ describe('Models', () => {
expect(schema.oneOf).toHaveLength(2); expect(schema.oneOf).toHaveLength(2);
expect(schema.displayType).toBe('(Array of strings or numbers) or string'); expect(schema.displayType).toBe('(Array of strings or numbers) or string');
}); });
test('expandSchemas != true', () => {
const spec = require('../fixtures/expandSchemas.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(parser, spec.components.schemas.Foo, '', opts);
expect(schema.fields).toHaveLength(2);
expect(schema.fields![0].expanded).toEqual(false);
expect(schema.fields![1].expanded).toEqual(false);
});
test('expandSchemas == true', () => {
const opts = new RedocNormalizedOptions({ expandSchemas: true});
const spec = require('../fixtures/expandSchemas.json');
parser = new OpenAPIParser(spec, undefined, opts);
const schema = new SchemaModel(parser, spec.components.schemas.Foo, '', opts);
expect(schema.fields).toHaveLength(2);
expect(schema.fields![0].expanded).toEqual(true);
expect(schema.fields![1].expanded).toEqual(true);
});
}); });
}); });

View File

@ -2,7 +2,10 @@ import { action, observable, makeObservable } from 'mobx';
import { OpenAPIResponse, Referenced } from '../../types'; import { OpenAPIResponse, Referenced } from '../../types';
import { getStatusCodeType } from '../../utils'; import {
extractExtensions,
getStatusCodeType,
} from '../../utils';
import { OpenAPIParser } from '../OpenAPIParser'; import { OpenAPIParser } from '../OpenAPIParser';
import { RedocNormalizedOptions } from '../RedocNormalizedOptions'; import { RedocNormalizedOptions } from '../RedocNormalizedOptions';
import { FieldModel } from './Field'; import { FieldModel } from './Field';
@ -19,6 +22,8 @@ export class ResponseModel {
type: string; type: string;
headers: FieldModel[] = []; headers: FieldModel[] = [];
extensions: Record<string, any>;
constructor( constructor(
parser: OpenAPIParser, parser: OpenAPIParser,
code: string, code: string,
@ -54,6 +59,10 @@ export class ResponseModel {
return new FieldModel(parser, { ...header, name }, '', options); return new FieldModel(parser, { ...header, name }, '', options);
}); });
} }
if (options.showExtensions) {
this.extensions = extractExtensions(info, options.showExtensions);
}
} }
@action @action

View File

@ -363,7 +363,7 @@ function buildFields(
const required = const required =
schema.required === undefined ? false : schema.required.indexOf(fieldName) > -1; schema.required === undefined ? false : schema.required.indexOf(fieldName) > -1;
return new FieldModel( const fieldModel = new FieldModel(
parser, parser,
{ {
name: fieldName, name: fieldName,
@ -376,6 +376,8 @@ function buildFields(
$ref + '/properties/' + fieldName, $ref + '/properties/' + fieldName,
options, options,
); );
fieldModel.expanded = options.expandSchemas;
return fieldModel;
}); });
if (options.sortPropsAlphabetically) { if (options.sortPropsAlphabetically) {
@ -387,22 +389,22 @@ function buildFields(
} }
if (typeof additionalProps === 'object' || additionalProps === true) { if (typeof additionalProps === 'object' || additionalProps === true) {
fields.push( const fieldModel = new FieldModel(
new FieldModel( parser,
parser, {
{ name: (typeof additionalProps === 'object'
name: (typeof additionalProps === 'object' ? additionalProps['x-additionalPropertiesName'] || 'property name'
? additionalProps['x-additionalPropertiesName'] || 'property name' : 'property name'
: 'property name' ).concat('*'),
).concat('*'), required: false,
required: false, schema: additionalProps === true ? {} : additionalProps,
schema: additionalProps === true ? {} : additionalProps, kind: 'additionalProperties',
kind: 'additionalProperties', },
}, $ref + '/additionalProperties',
$ref + '/additionalProperties', options,
options,
),
); );
fieldModel.expanded = options.expandSchemas;
fields.push(fieldModel);
} }
return fields; return fields;

View File

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