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 schema title and descriptions #1035

Closed
Closed
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
2 changes: 1 addition & 1 deletion src/components/Fields/Field.tsx
Original file line number Diff line number Diff line change
@@ -73,7 +73,7 @@ export class Field extends React.Component<FieldProps> {
schema={field.schema}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
skipObjectTitle={this.props.skipObjectTitle}
/>
</InnerPropertiesWrap>
</PropertyCellWithInner>
91 changes: 59 additions & 32 deletions src/components/Schema/ObjectSchema.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { observer } from 'mobx-react';
import * as React from 'react';

import styled from '../../styled-components';

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

import { PropertiesTable, PropertiesTableCaption } from '../../common-elements/fields-layout';
import { PropertiesTable } from '../../common-elements/fields-layout';
import { H3 } from '../../common-elements/headers';

import { Field } from '../Fields/Field';
import { Markdown } from '../Markdown/Markdown';
import { DiscriminatorDropdown } from './DiscriminatorDropdown';
import { SchemaProps } from './Schema';

@@ -17,6 +22,18 @@ 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;
`;

@observer
export class ObjectSchema extends React.Component<ObjectSchemaProps> {
get parentSchema() {
@@ -26,7 +43,6 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
render() {
const {
schema: { fields = [] },
showTitle,
discriminator,
} = this.props;

@@ -42,36 +58,47 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
: fields;

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

<PropertiesTable>
<tbody>
{mapWithLast(filteredFields, (field, isLast) => {
return (
<Field
key={field.name}
isLast={isLast}
field={field}
renderDiscriminatorSwitch={
(discriminator &&
discriminator.fieldName === field.name &&
(() => (
<DiscriminatorDropdown
parent={this.parentSchema}
enumValues={field.schema.enum}
/>
))) ||
undefined
}
className={field.expanded ? 'expanded' : undefined}
showExamples={false}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
/>
);
})}
</tbody>
</PropertiesTable>
</div>
);
}
}
11 changes: 5 additions & 6 deletions src/components/Schema/Schema.tsx
Original file line number Diff line number Diff line change
@@ -13,9 +13,10 @@ import { OneOfSchema } from './OneOfSchema';
import { l } from '../../services/Labels';

export interface SchemaOptions {
showTitle?: boolean;
skipReadOnly?: boolean;
skipWriteOnly?: boolean;
skipObjectTitle?: boolean;
skipObjectDescription?: boolean;
}

export interface SchemaProps extends SchemaOptions {
@@ -44,9 +45,7 @@ export class Schema extends React.Component<Partial<SchemaProps>> {
if (discriminatorProp !== undefined) {
if (!oneOf || !oneOf.length) {
throw new Error(
`Looks like you are using discriminator wrong: you don't have any definition inherited from the ${
schema.title
}`,
`Looks like you are using discriminator wrong: you don't have any definition inherited from the ${schema.title}`,
);
}
return (
@@ -66,9 +65,9 @@ export class Schema extends React.Component<Partial<SchemaProps>> {

switch (type) {
case 'object':
return <ObjectSchema {...this.props as any} />;
return <ObjectSchema {...(this.props as any)} />;
case 'array':
return <ArraySchema {...this.props as any} />;
return <ArraySchema {...(this.props as any)} />;
}

// TODO: maybe adjust FieldDetails to accept schema
11 changes: 10 additions & 1 deletion src/components/SchemaDefinition/SchemaDefinition.tsx
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ export interface ObjectDescriptionProps {
exampleRef?: string;
showReadOnly?: boolean;
showWriteOnly?: boolean;
showObjectTitle?: boolean;
showObjectDescription?: boolean;
parser: OpenAPIParser;
options: RedocNormalizedOptions;
}
@@ -53,14 +55,21 @@ export class SchemaDefinition extends React.PureComponent<ObjectDescriptionProps
}

render() {
const { showReadOnly = true, showWriteOnly = false } = this.props;
const {
showReadOnly = true,
showWriteOnly = false,
showObjectTitle = false,
showObjectDescription = false,
} = this.props;
return (
<Section>
<Row>
<MiddlePanel>
<Schema
skipWriteOnly={!showWriteOnly}
skipReadOnly={!showReadOnly}
skipObjectTitle={!showObjectTitle}
skipObjectDescription={!showObjectDescription}
schema={this.mediaModel.schema}
/>
</MiddlePanel>