Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally render object schema title and descriptions #1497

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Each deployment type has documentation on how to configure options for that type

**Versions: 2.x**

{% admonition type="success" name="Client-side configuration" %}
{% admonition type="success" name="Client-side configuration" %}

Using Redoc as a standalone (HTML or React) tool, these options must be presented in [kebab case](https://en.wikipedia.org/wiki/Letter_case#Kebab_case).
For example, `scrollYOffset` becomes `scroll-y-offset`, and `expandResponses` becomes `expand-responses`.
Expand Down Expand Up @@ -66,6 +66,14 @@ If set to `true`, the pattern is not shown in the schema.

Hides the schema title next to to the type.

### hideObjectTitle

Hides the object title in the schema.

### hideObjectDescription

Hides the object description in the schema.

### hideSecuritySection

Hides the Security panel section.
Expand Down
3 changes: 2 additions & 1 deletion src/components/Fields/Field.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ export class Field extends React.Component<FieldProps> {
schema={field.schema}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
hideObjectTitle={this.props.hideObjectTitle}
hideObjectDescription={this.props.hideObjectDescription}
level={this.props.level}
/>
</InnerPropertiesWrap>
Expand Down
12 changes: 11 additions & 1 deletion src/components/Parameters/Parameters.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as React from 'react';
import { DropdownOrLabel, DropdownOrLabelProps } from '../DropdownOrLabel/DropdownOrLabel';
import { ParametersGroup } from './ParametersGroup';
import { OptionsContext } from '../OptionsProvider';

import { UnderlinedHeader } from '../../common-elements';

Expand Down Expand Up @@ -29,6 +30,8 @@ export interface ParametersProps {
const PARAM_PLACES = ['path', 'query', 'cookie', 'header'];

export class Parameters extends React.PureComponent<ParametersProps> {
static contextType = OptionsContext;

orderParams(params: FieldModel[]): Record<string, FieldModel[]> {
const res = {};
params.forEach(param => {
Expand All @@ -38,6 +41,7 @@ export class Parameters extends React.PureComponent<ParametersProps> {
}

render() {
const { hideObjectTitle, hideObjectDescription } = this.context;
const { body, parameters = [] } = this.props;
if (body === undefined && parameters === undefined) {
return null;
Expand All @@ -63,6 +67,8 @@ export class Parameters extends React.PureComponent<ParametersProps> {
content={bodyContent}
description={bodyDescription}
bodyRequired={bodyRequired}
hideObjectTitle={hideObjectTitle}
hideObjectDescription={hideObjectDescription}
/>
)}
</>
Expand Down Expand Up @@ -90,8 +96,10 @@ export function BodyContent(props: {
content: MediaContentModel;
description?: string;
bodyRequired?: boolean;
hideObjectTitle?: boolean;
hideObjectDescription?: boolean;
}): JSX.Element {
const { content, description, bodyRequired } = props;
const { content, description, bodyRequired, hideObjectTitle, hideObjectDescription } = props;
const { isRequestType } = content;
return (
<MediaTypesSwitch
Expand All @@ -108,6 +116,8 @@ export function BodyContent(props: {
<Schema
skipReadOnly={isRequestType}
skipWriteOnly={!isRequestType}
hideObjectTitle={hideObjectTitle}
hideObjectDescription={hideObjectDescription}
key="schema"
schema={schema}
/>
Expand Down
12 changes: 11 additions & 1 deletion src/components/Responses/ResponseDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,13 @@ import { Extensions } from '../Fields/Extensions';
import { Markdown } from '../Markdown/Markdown';
import { ResponseHeaders } from './ResponseHeaders';
import { ConstraintsView } from '../Fields/FieldConstraints';
import { OptionsContext } from '../OptionsProvider';

export class ResponseDetails extends React.PureComponent<{ response: ResponseModel }> {
static contextType = OptionsContext;

render() {
const { hideObjectTitle, hideObjectDescription } = this.context;
const { description, extensions, headers, content } = this.props.response;
return (
<>
Expand All @@ -27,7 +31,13 @@ export class ResponseDetails extends React.PureComponent<{ response: ResponseMod
{schema?.type === 'object' && (
<ConstraintsView constraints={schema?.constraints || []} />
)}
<Schema skipWriteOnly={true} key="schema" schema={schema} />
<Schema
hideObjectTitle={hideObjectTitle}
hideObjectDescription={hideObjectDescription}
skipWriteOnly={true}
key="schema"
schema={schema}
/>
</>
);
}}
Expand Down
96 changes: 62 additions & 34 deletions src/components/Schema/ObjectSchema.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { observer } from 'mobx-react';
import * as React from 'react';

import styled from '../../styled-components';
import { H3 } from '../../common-elements/headers';
import { Markdown } from '../Markdown/Markdown';

import { SchemaModel } from '../../services/models';

import { PropertiesTable, PropertiesTableCaption } from '../../common-elements/fields-layout';
import { PropertiesTable } from '../../common-elements/fields-layout';
import { Field } from '../Fields/Field';
import { DiscriminatorDropdown } from './DiscriminatorDropdown';
import { SchemaProps } from './Schema';
Expand All @@ -18,13 +22,26 @@ export interface ObjectSchemaProps extends SchemaProps {
};
}

export const ObjectSchemaDetails = styled.div`
margin: 0 0 0.5em 0;
`;

export const ObjectSchemaTitle = styled(H3)`
margin: 0.5em 0 0 0;
`;

export const ObjectSchemaDescription = styled.div`
margin: 0.5em 0 0 0;
`;

export const ObjectSchema = observer(
({
schema: { fields = [], title },
showTitle,
schema: { fields = [], title, description },
discriminator,
skipReadOnly,
skipWriteOnly,
hideObjectTitle,
hideObjectDescription,
level,
}: ObjectSchemaProps) => {
const { expandSingleSchemaField, showObjectSchemaExamples, schemaExpansionLevel } =
Expand All @@ -48,37 +65,48 @@ export const ObjectSchema = observer(
(expandSingleSchemaField && filteredFields.length === 1) || schemaExpansionLevel >= level!;

return (
<PropertiesTable>
{showTitle && <PropertiesTableCaption>{title}</PropertiesTableCaption>}
<tbody>
{mapWithLast(filteredFields, (field, isLast) => {
return (
<Field
key={field.name}
isLast={isLast}
field={field}
expandByDefault={expandByDefault}
renderDiscriminatorSwitch={
discriminator?.fieldName === field.name
? () => (
<DiscriminatorDropdown
parent={discriminator!.parentSchema}
enumValues={field.schema.enum}
/>
)
: undefined
}
className={field.expanded ? 'expanded' : undefined}
showExamples={showObjectSchemaExamples}
skipReadOnly={skipReadOnly}
skipWriteOnly={skipWriteOnly}
showTitle={showTitle}
level={level}
/>
);
})}
</tbody>
</PropertiesTable>
<div>
<ObjectSchemaDetails>
{!hideObjectTitle && <ObjectSchemaTitle>{title}</ObjectSchemaTitle>}
{!hideObjectDescription && (
<ObjectSchemaDescription>
<Markdown compact={true} source={description} />
</ObjectSchemaDescription>
)}
</ObjectSchemaDetails>

<PropertiesTable>
<tbody>
{mapWithLast(filteredFields, (field, isLast) => {
return (
<Field
key={field.name}
isLast={isLast}
field={field}
expandByDefault={expandByDefault}
renderDiscriminatorSwitch={
discriminator?.fieldName === field.name
? () => (
<DiscriminatorDropdown
parent={discriminator!.parentSchema}
enumValues={field.schema.enum}
/>
)
: undefined
}
className={field.expanded ? 'expanded' : undefined}
showExamples={showObjectSchemaExamples}
skipReadOnly={skipReadOnly}
skipWriteOnly={skipWriteOnly}
hideObjectTitle={hideObjectTitle}
hideObjectDescription={hideObjectDescription}
level={level}
/>
);
})}
</tbody>
</PropertiesTable>
</div>
);
},
);
3 changes: 2 additions & 1 deletion src/components/Schema/Schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import { RecursiveSchema } from './RecursiveSchema';
import { isArray } from '../../utils/helpers';

export interface SchemaOptions {
showTitle?: boolean;
skipReadOnly?: boolean;
skipWriteOnly?: boolean;
hideObjectTitle?: boolean;
hideObjectDescription?: boolean;
level?: number;
}

Expand Down
12 changes: 11 additions & 1 deletion src/components/SchemaDefinition/SchemaDefinition.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface ObjectDescriptionProps {
exampleRef?: string;
showReadOnly?: boolean;
showWriteOnly?: boolean;
showObjectTitle?: boolean;
showObjectDescription?: boolean;
showExample?: boolean;
parser: OpenAPIParser;
options: RedocNormalizedOptions;
Expand Down Expand Up @@ -54,14 +56,22 @@ export class SchemaDefinition extends React.PureComponent<ObjectDescriptionProps
}

render() {
const { showReadOnly = true, showWriteOnly = false, showExample = true } = this.props;
const {
showReadOnly = true,
showWriteOnly = false,
showExample = true,
showObjectTitle = false,
showObjectDescription = false,
} = this.props;
return (
<Section>
<Row>
<MiddlePanel>
<Schema
skipWriteOnly={!showWriteOnly}
skipReadOnly={!showReadOnly}
hideObjectTitle={!showObjectTitle}
hideObjectDescription={!showObjectDescription}
schema={this.mediaModel.schema}
/>
</MiddlePanel>
Expand Down
Loading