From c986f0ef1a38bc1e61cae70830d84de03b684b89 Mon Sep 17 00:00:00 2001 From: Oprysk Vyacheslav Date: Wed, 29 Dec 2021 12:22:57 +0200 Subject: [PATCH] feat: support examples in object schema (#1832) * feat: support examples in object schema * revert pure components used with observer * rename option * update test --- src/components/Schema/ObjectSchema.tsx | 79 +- .../__tests__/DiscriminatorDropdown.test.tsx | 2 +- .../DiscriminatorDropdown.test.tsx.snap | 2448 +++++++++++++++++ src/services/RedocNormalizedOptions.ts | 3 + 4 files changed, 2490 insertions(+), 42 deletions(-) diff --git a/src/components/Schema/ObjectSchema.tsx b/src/components/Schema/ObjectSchema.tsx index 66f6034a..e115eb85 100644 --- a/src/components/Schema/ObjectSchema.tsx +++ b/src/components/Schema/ObjectSchema.tsx @@ -18,37 +18,35 @@ export interface ObjectSchemaProps extends SchemaProps { }; } -@observer -export class ObjectSchema extends React.Component { - static contextType = OptionsContext; +export const ObjectSchema = observer( + ({ + schema: { fields = [], title }, + showTitle, + discriminator, + skipReadOnly, + skipWriteOnly, + }: ObjectSchemaProps) => { + const { expandSingleSchemaField, showObjectSchemaExamples } = React.useContext(OptionsContext); - get parentSchema() { - return this.props.discriminator!.parentSchema; - } + const filteredFields = React.useMemo( + () => + skipReadOnly || skipWriteOnly + ? fields.filter( + item => + !( + (skipReadOnly && item.schema.readOnly) || + (skipWriteOnly && item.schema.writeOnly) + ), + ) + : fields, + [skipReadOnly, skipWriteOnly, fields], + ); - render() { - const { - schema: { fields = [] }, - showTitle, - discriminator, - } = this.props; - - 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) - ); - }) - : fields; - - const expandByDefault = this.context.expandSingleSchemaField && filteredFields.length === 1; + const expandByDefault = expandSingleSchemaField && filteredFields.length === 1; return ( - {showTitle && {this.props.schema.title}} + {showTitle && {title}} {mapWithLast(filteredFields, (field, isLast) => { return ( @@ -58,26 +56,25 @@ export class ObjectSchema extends React.Component { field={field} expandByDefault={expandByDefault} renderDiscriminatorSwitch={ - (discriminator && - discriminator.fieldName === field.name && - (() => ( - - ))) || - undefined + discriminator?.fieldName === field.name + ? () => ( + + ) + : undefined } className={field.expanded ? 'expanded' : undefined} - showExamples={false} - skipReadOnly={this.props.skipReadOnly} - skipWriteOnly={this.props.skipWriteOnly} - showTitle={this.props.showTitle} + showExamples={showObjectSchemaExamples} + skipReadOnly={skipReadOnly} + skipWriteOnly={skipWriteOnly} + showTitle={showTitle} /> ); })} ); - } -} + }, +); diff --git a/src/components/__tests__/DiscriminatorDropdown.test.tsx b/src/components/__tests__/DiscriminatorDropdown.test.tsx index 6c02d1c6..1a625b94 100644 --- a/src/components/__tests__/DiscriminatorDropdown.test.tsx +++ b/src/components/__tests__/DiscriminatorDropdown.test.tsx @@ -26,7 +26,7 @@ describe('Components', () => { options, ); const schemaViewElement = shallow().getElement(); - expect(schemaViewElement.type).toEqual(ObjectSchema); + expect(schemaViewElement).toMatchSnapshot(); expect(schemaViewElement.props.discriminator).toBeDefined(); expect(schemaViewElement.props.discriminator.parentSchema).toBeDefined(); expect(schemaViewElement.props.discriminator.fieldName).toEqual('type'); diff --git a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap index 7af7b482..ccfa8185 100644 --- a/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap +++ b/src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap @@ -1,9 +1,2456 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`Components SchemaView discriminator should correctly render SchemaView 1`] = ` + +`; + exports[`Components SchemaView discriminator should correctly render discriminator dropdown 1`] = `