feat: support examples in object schema (#1832)

* feat: support examples in object schema

* revert pure components used with observer

* rename option

* update test
This commit is contained in:
Oprysk Vyacheslav 2021-12-29 12:22:57 +02:00 committed by GitHub
parent 6c41e95aa0
commit c986f0ef1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 2490 additions and 42 deletions

View File

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

View File

@ -26,7 +26,7 @@ describe('Components', () => {
options, options,
); );
const schemaViewElement = shallow(<Schema schema={schema} />).getElement(); const schemaViewElement = shallow(<Schema schema={schema} />).getElement();
expect(schemaViewElement.type).toEqual(ObjectSchema); expect(schemaViewElement).toMatchSnapshot();
expect(schemaViewElement.props.discriminator).toBeDefined(); expect(schemaViewElement.props.discriminator).toBeDefined();
expect(schemaViewElement.props.discriminator.parentSchema).toBeDefined(); expect(schemaViewElement.props.discriminator.parentSchema).toBeDefined();
expect(schemaViewElement.props.discriminator.fieldName).toEqual('type'); expect(schemaViewElement.props.discriminator.fieldName).toEqual('type');

View File

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