fix: add schema expansion level (#1868)

* feat: add schemaExpansionLevel
This commit is contained in:
AlexVarchuk 2022-01-25 16:43:14 +02:00 committed by GitHub
parent 35418b1569
commit 250d53a59f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 49 additions and 17 deletions

View File

@ -32,7 +32,7 @@ export interface FieldProps extends SchemaOptions {
export class Field extends React.Component<FieldProps> { export class Field extends React.Component<FieldProps> {
toggle = () => { toggle = () => {
if (this.props.field.expanded === undefined && this.props.expandByDefault) { if (this.props.field.expanded === undefined && this.props.expandByDefault) {
this.props.field.expanded = false; this.props.field.collapse();
} else { } else {
this.props.field.toggle(); this.props.field.toggle();
} }
@ -94,6 +94,7 @@ export class Field extends React.Component<FieldProps> {
skipReadOnly={this.props.skipReadOnly} skipReadOnly={this.props.skipReadOnly}
skipWriteOnly={this.props.skipWriteOnly} skipWriteOnly={this.props.skipWriteOnly}
showTitle={this.props.showTitle} showTitle={this.props.showTitle}
level={this.props.level}
/> />
</InnerPropertiesWrap> </InnerPropertiesWrap>
</PropertyCellWithInner> </PropertyCellWithInner>

View File

@ -25,8 +25,10 @@ export const ObjectSchema = observer(
discriminator, discriminator,
skipReadOnly, skipReadOnly,
skipWriteOnly, skipWriteOnly,
level,
}: ObjectSchemaProps) => { }: ObjectSchemaProps) => {
const { expandSingleSchemaField, showObjectSchemaExamples } = React.useContext(OptionsContext); const { expandSingleSchemaField, showObjectSchemaExamples, schemaExpansionLevel } =
React.useContext(OptionsContext);
const filteredFields = React.useMemo( const filteredFields = React.useMemo(
() => () =>
@ -42,7 +44,8 @@ export const ObjectSchema = observer(
[skipReadOnly, skipWriteOnly, fields], [skipReadOnly, skipWriteOnly, fields],
); );
const expandByDefault = expandSingleSchemaField && filteredFields.length === 1; const expandByDefault =
(expandSingleSchemaField && filteredFields.length === 1) || schemaExpansionLevel >= level!;
return ( return (
<PropertiesTable> <PropertiesTable>
@ -70,6 +73,7 @@ export const ObjectSchema = observer(
skipReadOnly={skipReadOnly} skipReadOnly={skipReadOnly}
skipWriteOnly={skipWriteOnly} skipWriteOnly={skipWriteOnly}
showTitle={showTitle} showTitle={showTitle}
level={level}
/> />
); );
})} })}

View File

@ -16,6 +16,7 @@ export interface SchemaOptions {
showTitle?: boolean; showTitle?: boolean;
skipReadOnly?: boolean; skipReadOnly?: boolean;
skipWriteOnly?: boolean; skipWriteOnly?: boolean;
level?: number;
} }
export interface SchemaProps extends SchemaOptions { export interface SchemaProps extends SchemaOptions {
@ -25,7 +26,9 @@ export interface SchemaProps extends SchemaOptions {
@observer @observer
export class Schema extends React.Component<Partial<SchemaProps>> { export class Schema extends React.Component<Partial<SchemaProps>> {
render() { render() {
const { schema } = this.props; const { schema, ...rest } = this.props;
const level = (rest.level || 0) + 1;
if (!schema) { if (!schema) {
return <em> Schema not provided </em>; return <em> Schema not provided </em>;
} }
@ -50,7 +53,9 @@ export class Schema extends React.Component<Partial<SchemaProps>> {
} }
return ( return (
<ObjectSchema <ObjectSchema
{...{ ...this.props, schema: oneOf![schema.activeOneOf] }} {...rest}
level={level}
schema={oneOf![schema.activeOneOf]}
discriminator={{ discriminator={{
fieldName: discriminatorProp, fieldName: discriminatorProp,
parentSchema: schema, parentSchema: schema,
@ -60,16 +65,16 @@ export class Schema extends React.Component<Partial<SchemaProps>> {
} }
if (oneOf !== undefined) { if (oneOf !== undefined) {
return <OneOfSchema schema={schema} {...this.props} />; return <OneOfSchema schema={schema} {...rest} />;
} }
const types = Array.isArray(type) ? type : [type]; const types = Array.isArray(type) ? type : [type];
if (types.includes('object')) { if (types.includes('object')) {
if (schema.fields?.length) { if (schema.fields?.length) {
return <ObjectSchema {...(this.props as any)} />; return <ObjectSchema {...(this.props as any)} level={level} />;
} }
} else if (types.includes('array')) { } else if (types.includes('array')) {
return <ArraySchema {...(this.props as any)} />; return <ArraySchema {...(this.props as any)} level={level} />;
} }
// TODO: maybe adjust FieldDetails to accept schema // TODO: maybe adjust FieldDetails to accept schema

View File

@ -46,7 +46,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",
@ -94,6 +93,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -285,7 +285,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",
@ -333,6 +332,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -548,6 +548,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -780,7 +781,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",
@ -828,6 +828,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -1019,7 +1020,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",
@ -1067,6 +1067,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -1282,6 +1283,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -1520,6 +1522,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -1727,6 +1730,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
}, },
} }
} }
level={1}
schema={ schema={
SchemaModel { SchemaModel {
"activeOneOf": 0, "activeOneOf": 0,
@ -1748,7 +1752,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",
@ -1796,6 +1799,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -1987,7 +1991,6 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",
@ -2035,6 +2038,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -2250,6 +2254,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
"pathInMiddlePanel": false, "pathInMiddlePanel": false,
"payloadSampleIdx": 0, "payloadSampleIdx": 0,
"requiredPropsFirst": false, "requiredPropsFirst": false,
"schemaExpansionLevel": 0,
"scrollYOffset": [Function], "scrollYOffset": [Function],
"showExtensions": false, "showExtensions": false,
"showObjectSchemaExamples": false, "showObjectSchemaExamples": false,
@ -2477,7 +2482,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",
@ -2533,7 +2537,6 @@ exports[`Components SchemaView discriminator should correctly render discriminat
"deprecated": false, "deprecated": false,
"description": "", "description": "",
"example": undefined, "example": undefined,
"expanded": false,
"explode": false, "explode": false,
"in": undefined, "in": undefined,
"kind": "field", "kind": "field",

View File

@ -37,6 +37,7 @@ export interface RedocRawOptions {
simpleOneOfTypeLabel?: boolean | string; simpleOneOfTypeLabel?: boolean | string;
payloadSampleIdx?: number; payloadSampleIdx?: number;
expandSingleSchemaField?: boolean | string; expandSingleSchemaField?: boolean | string;
schemaExpansionLevel?: number | string | 'all';
showObjectSchemaExamples?: boolean | string; showObjectSchemaExamples?: boolean | string;
unstable_ignoreMimeParameters?: boolean; unstable_ignoreMimeParameters?: boolean;
@ -74,6 +75,12 @@ function argValueToNumber(value: number | string | undefined): number | undefine
} }
} }
function argValueToExpandLevel(value?: number | string | undefined, defaultValue = 0): number {
if (value === 'all') return Infinity;
return argValueToNumber(value) || defaultValue;
}
export class RedocNormalizedOptions { export class RedocNormalizedOptions {
static normalizeExpandResponses(value: RedocRawOptions['expandResponses']) { static normalizeExpandResponses(value: RedocRawOptions['expandResponses']) {
if (value === 'all') { if (value === 'all') {
@ -225,6 +232,7 @@ export class RedocNormalizedOptions {
simpleOneOfTypeLabel: boolean; simpleOneOfTypeLabel: boolean;
payloadSampleIdx: number; payloadSampleIdx: number;
expandSingleSchemaField: boolean; expandSingleSchemaField: boolean;
schemaExpansionLevel: number;
showObjectSchemaExamples: boolean; showObjectSchemaExamples: boolean;
/* tslint:disable-next-line */ /* tslint:disable-next-line */
@ -289,6 +297,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.schemaExpansionLevel = argValueToExpandLevel(raw.schemaExpansionLevel);
this.showObjectSchemaExamples = argValueToBoolean(raw.showObjectSchemaExamples); this.showObjectSchemaExamples = argValueToBoolean(raw.showObjectSchemaExamples);
this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters); this.unstable_ignoreMimeParameters = argValueToBoolean(raw.unstable_ignoreMimeParameters);

View File

@ -41,7 +41,7 @@ const DEFAULT_SERIALIZATION: Record<
*/ */
export class FieldModel { export class FieldModel {
@observable @observable
expanded: boolean | undefined = false; expanded: boolean | undefined;
schema: SchemaModel; schema: SchemaModel;
name: string; name: string;
@ -120,4 +120,14 @@ export class FieldModel {
toggle() { toggle() {
this.expanded = !this.expanded; this.expanded = !this.expanded;
} }
@action
collapse(): void {
this.expanded = false;
}
@action
expand(): void {
this.expanded = true;
}
} }