diff --git a/README.md b/README.md index a00994c1..a940b6d6 100644 --- a/README.md +++ b/README.md @@ -230,6 +230,7 @@ You can use all of the following options with standalone version on tag * `hideHostname` - if set, the protocol and hostname is not shown in the operation definition. * `hideLoading` - do not show loading animation. Useful for small docs. * `hideSingleRequestSampleTab` - do not show the request sample tab for requests with only one sample. +* `expandSingleSchemaField` - automatically expand single field in a schema * `jsonSampleExpandLevel` - set the default expand level for JSON payload samples (responses and request body). Special value 'all' expands all levels. The default value is `2`. * `lazyRendering` - _Not implemented yet_ ~~if set, enables lazy rendering mode in ReDoc. This mode is useful for APIs with big number of operations (e.g. > 50). In this mode ReDoc shows initial screen ASAP and then renders the rest operations asynchronously while showing progress bar on the top. Check out the [demo](\\redocly.github.io/redoc) for the example.~~ * `menuToggle` - if true clicking second time on expanded menu item will collapse it, default `false`. diff --git a/src/components/Fields/Field.tsx b/src/components/Fields/Field.tsx index ef969b2d..d1584b23 100644 --- a/src/components/Fields/Field.tsx +++ b/src/components/Fields/Field.tsx @@ -23,6 +23,7 @@ export interface FieldProps extends SchemaOptions { showExamples?: boolean; field: FieldModel; + expandByDefault?: boolean; renderDiscriminatorSwitch?: (opts: FieldProps) => JSX.Element; } @@ -30,13 +31,19 @@ export interface FieldProps extends SchemaOptions { @observer export class Field extends React.Component { toggle = () => { - this.props.field.toggle(); + if (this.props.field.expanded === undefined && this.props.expandByDefault) { + this.props.field.expanded = false; + } else { + this.props.field.toggle(); + } }; render() { - const { className, field, isLast } = this.props; - const { name, expanded, deprecated, required, kind } = field; + const { className, field, isLast, expandByDefault } = this.props; + const { name, deprecated, required, kind } = field; const withSubSchema = !field.schema.isPrimitive && !field.schema.isCircular; + const expanded = field.expanded === undefined ? expandByDefault : field.expanded; + const paramName = withSubSchema ? ( { - {field.expanded && withSubSchema && ( + {expanded && withSubSchema && ( diff --git a/src/components/Schema/ObjectSchema.tsx b/src/components/Schema/ObjectSchema.tsx index 3bad4e47..66f6034a 100644 --- a/src/components/Schema/ObjectSchema.tsx +++ b/src/components/Schema/ObjectSchema.tsx @@ -9,6 +9,7 @@ import { DiscriminatorDropdown } from './DiscriminatorDropdown'; import { SchemaProps } from './Schema'; import { mapWithLast } from '../../utils'; +import { OptionsContext } from '../OptionsProvider'; export interface ObjectSchemaProps extends SchemaProps { discriminator?: { @@ -19,6 +20,8 @@ export interface ObjectSchemaProps extends SchemaProps { @observer export class ObjectSchema extends React.Component { + static contextType = OptionsContext; + get parentSchema() { return this.props.discriminator!.parentSchema; } @@ -41,6 +44,8 @@ export class ObjectSchema extends React.Component { }) : fields; + const expandByDefault = this.context.expandSingleSchemaField && filteredFields.length === 1; + return ( {showTitle && {this.props.schema.title}} @@ -51,6 +56,7 @@ export class ObjectSchema extends React.Component { key={field.name} isLast={isLast} field={field} + expandByDefault={expandByDefault} renderDiscriminatorSwitch={ (discriminator && discriminator.fieldName === field.name && diff --git a/src/services/RedocNormalizedOptions.ts b/src/services/RedocNormalizedOptions.ts index f0850351..4ae12dce 100644 --- a/src/services/RedocNormalizedOptions.ts +++ b/src/services/RedocNormalizedOptions.ts @@ -26,6 +26,7 @@ export interface RedocRawOptions { jsonSampleExpandLevel?: number | string | 'all'; hideSchemaTitles?: boolean | string; payloadSampleIdx?: number; + expandSingleSchemaField?: boolean | string; unstable_ignoreMimeParameters?: boolean; @@ -160,6 +161,7 @@ export class RedocNormalizedOptions { enumSkipQuotes: boolean; hideSchemaTitles: boolean; payloadSampleIdx: number; + expandSingleSchemaField: boolean; /* tslint:disable-next-line */ unstable_ignoreMimeParameters: boolean; @@ -200,6 +202,7 @@ export class RedocNormalizedOptions { this.enumSkipQuotes = argValueToBoolean(raw.enumSkipQuotes); this.hideSchemaTitles = argValueToBoolean(raw.hideSchemaTitles); this.payloadSampleIdx = RedocNormalizedOptions.normalizePayloadSampleIdx(raw.payloadSampleIdx); + this.expandSingleSchemaField = argValueToBoolean(raw.expandSingleSchemaField); this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters); diff --git a/src/services/models/Field.ts b/src/services/models/Field.ts index 5302a09f..f22a86ae 100644 --- a/src/services/models/Field.ts +++ b/src/services/models/Field.ts @@ -30,7 +30,7 @@ function getDefaultStyleValue(parameterLocation: OpenAPIParameterLocation): Open */ export class FieldModel { @observable - expanded: boolean = false; + expanded: boolean | undefined; schema: SchemaModel; name: string;