feat: support examples in object schema

This commit is contained in:
Oprysk 2021-12-14 12:00:22 +02:00
parent 6c41e95aa0
commit 5f618c2979
5 changed files with 48 additions and 44 deletions

View File

@ -29,7 +29,7 @@ export interface FieldProps extends SchemaOptions {
}
@observer
export class Field extends React.Component<FieldProps> {
export class Field extends React.PureComponent<FieldProps> {
toggle = () => {
if (this.props.field.expanded === undefined && this.props.expandByDefault) {
this.props.field.expanded = false;

View File

@ -18,37 +18,35 @@ export interface ObjectSchemaProps extends SchemaProps {
};
}
@observer
export class ObjectSchema extends React.Component<ObjectSchemaProps> {
static contextType = OptionsContext;
get parentSchema() {
return this.props.discriminator!.parentSchema;
}
render() {
const {
schema: { fields = [] },
export const ObjectSchemaComponent = observer(
({
schema: { fields = [], title },
showTitle,
discriminator,
} = this.props;
skipReadOnly,
skipWriteOnly,
}: ObjectSchemaProps) => {
const { expandSingleSchemaField, showObjectExample } = React.useContext(OptionsContext);
const needFilter = this.props.skipReadOnly || this.props.skipWriteOnly;
const filteredFields = needFilter
? fields.filter(item => {
return !(
(this.props.skipReadOnly && item.schema.readOnly) ||
(this.props.skipWriteOnly && item.schema.writeOnly)
const filteredFields = React.useMemo(
() =>
skipReadOnly || skipWriteOnly
? fields.filter(
item =>
!(
(skipReadOnly && item.schema.readOnly) ||
(skipWriteOnly && item.schema.writeOnly)
),
)
: fields,
[skipReadOnly, skipWriteOnly, fields],
);
})
: fields;
const expandByDefault = this.context.expandSingleSchemaField && filteredFields.length === 1;
const expandByDefault = expandSingleSchemaField && filteredFields.length === 1;
return (
<PropertiesTable>
{showTitle && <PropertiesTableCaption>{this.props.schema.title}</PropertiesTableCaption>}
{showTitle && <PropertiesTableCaption>{title}</PropertiesTableCaption>}
<tbody>
{mapWithLast(filteredFields, (field, isLast) => {
return (
@ -58,26 +56,27 @@ export class ObjectSchema extends React.Component<ObjectSchemaProps> {
field={field}
expandByDefault={expandByDefault}
renderDiscriminatorSwitch={
(discriminator &&
discriminator.fieldName === field.name &&
(() => (
discriminator?.fieldName === field.name
? () => (
<DiscriminatorDropdown
parent={this.parentSchema}
parent={discriminator!.parentSchema}
enumValues={field.schema.enum}
/>
))) ||
undefined
)
: undefined
}
className={field.expanded ? 'expanded' : undefined}
showExamples={false}
skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle}
showExamples={showObjectExample}
skipReadOnly={skipReadOnly}
skipWriteOnly={skipWriteOnly}
showTitle={showTitle}
/>
);
})}
</tbody>
</PropertiesTable>
);
}
}
},
);
export const ObjectSchema = React.memo<ObjectSchemaProps>(ObjectSchemaComponent);

View File

@ -7,7 +7,7 @@ import * as React from 'react';
import { filterPropsDeep } from '../../utils/test-utils';
import { ObjectSchema, Schema } from '../';
import { ObjectSchemaComponent, ObjectSchema, Schema } from '../';
import { OpenAPIParser, SchemaModel } from '../../services';
import { RedocNormalizedOptions } from '../../services/RedocNormalizedOptions';
import * as simpleDiscriminatorFixture from './fixtures/simple-discriminator.json';
@ -26,7 +26,7 @@ describe('Components', () => {
options,
);
const schemaViewElement = shallow(<Schema schema={schema} />).getElement();
expect(schemaViewElement.type).toEqual(ObjectSchema);
expect(schemaViewElement.type).toEqual(ObjectSchemaComponent);
expect(schemaViewElement.props.discriminator).toBeDefined();
expect(schemaViewElement.props.discriminator.parentSchema).toBeDefined();
expect(schemaViewElement.props.discriminator.fieldName).toEqual('type');

View File

@ -4,6 +4,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
<styled.table>
<tbody>
<Field
expandByDefault={false}
field={
FieldModel {
"const": "",
@ -59,6 +60,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
showExamples={false}
/>
<Field
expandByDefault={false}
field={
FieldModel {
"const": "",

View File

@ -35,6 +35,7 @@ export interface RedocRawOptions {
simpleOneOfTypeLabel?: boolean | string;
payloadSampleIdx?: number;
expandSingleSchemaField?: boolean | string;
showObjectExample?: boolean | string;
unstable_ignoreMimeParameters?: boolean;
@ -220,6 +221,7 @@ export class RedocNormalizedOptions {
simpleOneOfTypeLabel: boolean;
payloadSampleIdx: number;
expandSingleSchemaField: boolean;
showObjectExample: boolean;
/* tslint:disable-next-line */
unstable_ignoreMimeParameters: boolean;
@ -281,6 +283,7 @@ export class RedocNormalizedOptions {
this.simpleOneOfTypeLabel = argValueToBoolean(raw.simpleOneOfTypeLabel);
this.payloadSampleIdx = RedocNormalizedOptions.normalizePayloadSampleIdx(raw.payloadSampleIdx);
this.expandSingleSchemaField = argValueToBoolean(raw.expandSingleSchemaField);
this.showObjectExample = argValueToBoolean(raw.showObjectExample);
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);