Skip to content

Commit 93d6b6c

Browse files
committed
feat: add support hidePropertiesPrefix
1 parent 349a194 commit 93d6b6c

File tree

7 files changed

+50
-7
lines changed

7 files changed

+50
-7
lines changed

src/components/Fields/Field.tsx

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { Schema } from '../Schema/Schema';
1919

2020
import type { SchemaOptions } from '../Schema/Schema';
2121
import type { FieldModel } from '../../services/models';
22+
import { OptionsContext } from '../OptionsProvider';
23+
import { RedocNormalizedOptions } from '../../services/RedocNormalizedOptions';
2224

2325
export interface FieldProps extends SchemaOptions {
2426
className?: string;
@@ -27,12 +29,15 @@ export interface FieldProps extends SchemaOptions {
2729

2830
field: FieldModel;
2931
expandByDefault?: boolean;
30-
32+
fieldParentsName?: string[];
3133
renderDiscriminatorSwitch?: (opts: FieldProps) => JSX.Element;
3234
}
3335

3436
@observer
3537
export class Field extends React.Component<FieldProps> {
38+
static contextType = OptionsContext;
39+
context: RedocNormalizedOptions;
40+
3641
toggle = () => {
3742
if (this.props.field.expanded === undefined && this.props.expandByDefault) {
3843
this.props.field.collapse();
@@ -49,12 +54,12 @@ export class Field extends React.Component<FieldProps> {
4954
};
5055

5156
render() {
52-
const { className = '', field, isLast, expandByDefault } = this.props;
57+
const { hidePropertiesPrefix } = this.context;
58+
const { className = '', field, isLast, expandByDefault, fieldParentsName = [] } = this.props;
5359
const { name, deprecated, required, kind } = field;
5460
const withSubSchema = !field.schema.isPrimitive && !field.schema.isCircular;
5561

5662
const expanded = field.expanded === undefined ? expandByDefault : field.expanded;
57-
5863
const labels = (
5964
<>
6065
{kind === 'additionalProperties' && <PropertyLabel>additional property</PropertyLabel>}
@@ -75,6 +80,10 @@ export class Field extends React.Component<FieldProps> {
7580
onKeyPress={this.handleKeyPress}
7681
aria-label={`expand ${name}`}
7782
>
83+
{!hidePropertiesPrefix &&
84+
fieldParentsName.map(
85+
name => name + '.\u200B', // zero-width space, a special character is used for correct line breaking
86+
)}
7887
<span className="property-name">{name}</span>
7988
<ShelfIcon direction={expanded ? 'down' : 'right'} />
8089
</button>
@@ -83,6 +92,10 @@ export class Field extends React.Component<FieldProps> {
8392
) : (
8493
<PropertyNameCell className={deprecated ? 'deprecated' : undefined} kind={kind} title={name}>
8594
<PropertyBullet />
95+
{!hidePropertiesPrefix &&
96+
fieldParentsName.map(
97+
name => name + '.\u200B', // zero-width space, a special character is used for correct line breaking
98+
)}
8699
<span className="property-name">{name}</span>
87100
{labels}
88101
</PropertyNameCell>
@@ -102,6 +115,7 @@ export class Field extends React.Component<FieldProps> {
102115
<InnerPropertiesWrap>
103116
<Schema
104117
schema={field.schema}
118+
fieldParentsName={[...(fieldParentsName || []), field.name]}
105119
skipReadOnly={this.props.skipReadOnly}
106120
skipWriteOnly={this.props.skipWriteOnly}
107121
showTitle={this.props.showTitle}

src/components/Schema/ArraySchema.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ export class ArraySchema extends React.PureComponent<SchemaProps> {
1616
render() {
1717
const schema = this.props.schema;
1818
const itemsSchema = schema.items;
19+
const fieldParentsName = this.props.fieldParentsName;
1920

2021
const minMaxItems =
2122
schema.minItems === undefined && schema.maxItems === undefined
2223
? ''
2324
: `(${humanizeConstraints(schema)})`;
2425

26+
const updatedParentsArray = fieldParentsName
27+
? [...fieldParentsName.slice(0, -1), fieldParentsName[fieldParentsName.length - 1] + '[]']
28+
: fieldParentsName;
2529
if (schema.fields) {
26-
return <ObjectSchema {...(this.props as any)} level={this.props.level} />;
30+
return (
31+
<ObjectSchema
32+
{...(this.props as any)}
33+
level={this.props.level}
34+
fieldParentsName={updatedParentsArray}
35+
/>
36+
);
2737
}
2838
if (schema.displayType && !itemsSchema && !minMaxItems.length) {
2939
return (
@@ -37,7 +47,7 @@ export class ArraySchema extends React.PureComponent<SchemaProps> {
3747
<div>
3848
<ArrayOpenningLabel> Array {minMaxItems}</ArrayOpenningLabel>
3949
<PaddedSchema>
40-
<Schema {...this.props} schema={itemsSchema} />
50+
<Schema {...this.props} schema={itemsSchema} fieldParentsName={updatedParentsArray} />
4151
</PaddedSchema>
4252
<ArrayClosingLabel />
4353
</div>

src/components/Schema/ObjectSchema.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export interface ObjectSchemaProps extends SchemaProps {
1616
fieldName: string;
1717
parentSchema: SchemaModel;
1818
};
19+
fieldParentsName?: string[];
1920
}
2021

2122
export const ObjectSchema = observer(
@@ -26,6 +27,7 @@ export const ObjectSchema = observer(
2627
skipReadOnly,
2728
skipWriteOnly,
2829
level,
30+
fieldParentsName,
2931
}: ObjectSchemaProps) => {
3032
const { expandSingleSchemaField, showObjectSchemaExamples, schemasExpansionLevel } =
3133
React.useContext(OptionsContext);
@@ -58,6 +60,7 @@ export const ObjectSchema = observer(
5860
isLast={isLast}
5961
field={field}
6062
expandByDefault={expandByDefault}
63+
fieldParentsName={Number(level) > 1 ? fieldParentsName : []}
6164
renderDiscriminatorSwitch={
6265
discriminator?.fieldName === field.name
6366
? () => (

src/components/Schema/Schema.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface SchemaOptions {
2121

2222
export interface SchemaProps extends SchemaOptions {
2323
schema: SchemaModel;
24+
fieldParentsName?: string[];
2425
}
2526

2627
@observer

src/components/__tests__/__snapshots__/DiscriminatorDropdown.test.tsx.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
8888
"hideDownloadButtons": false,
8989
"hideFab": false,
9090
"hideHostname": false,
91+
"hidePropertiesPrefix": true,
9192
"hideRequestPayloadSample": false,
9293
"hideSchemaPattern": false,
9394
"hideSchemaTitles": false,
@@ -363,6 +364,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
363364
"hideDownloadButtons": false,
364365
"hideFab": false,
365366
"hideHostname": false,
367+
"hidePropertiesPrefix": true,
366368
"hideRequestPayloadSample": false,
367369
"hideSchemaPattern": false,
368370
"hideSchemaTitles": false,
@@ -625,6 +627,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
625627
"hideDownloadButtons": false,
626628
"hideFab": false,
627629
"hideHostname": false,
630+
"hidePropertiesPrefix": true,
628631
"hideRequestPayloadSample": false,
629632
"hideSchemaPattern": false,
630633
"hideSchemaTitles": false,
@@ -949,6 +952,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
949952
"hideDownloadButtons": false,
950953
"hideFab": false,
951954
"hideHostname": false,
955+
"hidePropertiesPrefix": true,
952956
"hideRequestPayloadSample": false,
953957
"hideSchemaPattern": false,
954958
"hideSchemaTitles": false,
@@ -1236,6 +1240,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
12361240
"hideDownloadButtons": false,
12371241
"hideFab": false,
12381242
"hideHostname": false,
1243+
"hidePropertiesPrefix": true,
12391244
"hideRequestPayloadSample": false,
12401245
"hideSchemaPattern": false,
12411246
"hideSchemaTitles": false,
@@ -1494,6 +1499,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
14941499
"hideDownloadButtons": false,
14951500
"hideFab": false,
14961501
"hideHostname": false,
1502+
"hidePropertiesPrefix": true,
14971503
"hideRequestPayloadSample": false,
14981504
"hideSchemaPattern": false,
14991505
"hideSchemaTitles": false,
@@ -1777,6 +1783,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
17771783
"hideDownloadButtons": false,
17781784
"hideFab": false,
17791785
"hideHostname": false,
1786+
"hidePropertiesPrefix": true,
17801787
"hideRequestPayloadSample": false,
17811788
"hideSchemaPattern": false,
17821789
"hideSchemaTitles": false,
@@ -2090,6 +2097,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
20902097
"hideDownloadButtons": false,
20912098
"hideFab": false,
20922099
"hideHostname": false,
2100+
"hidePropertiesPrefix": true,
20932101
"hideRequestPayloadSample": false,
20942102
"hideSchemaPattern": false,
20952103
"hideSchemaTitles": false,
@@ -2365,6 +2373,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
23652373
"hideDownloadButtons": false,
23662374
"hideFab": false,
23672375
"hideHostname": false,
2376+
"hidePropertiesPrefix": true,
23682377
"hideRequestPayloadSample": false,
23692378
"hideSchemaPattern": false,
23702379
"hideSchemaTitles": false,
@@ -2627,6 +2636,7 @@ exports[`Components SchemaView discriminator should correctly render SchemaView
26272636
"hideDownloadButtons": false,
26282637
"hideFab": false,
26292638
"hideHostname": false,
2639+
"hidePropertiesPrefix": true,
26302640
"hideRequestPayloadSample": false,
26312641
"hideSchemaPattern": false,
26322642
"hideSchemaTitles": false,
@@ -2955,6 +2965,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
29552965
},
29562966
}
29572967
}
2968+
fieldParentsName={[]}
29582969
isLast={false}
29592970
key="packSize"
29602971
showExamples={false}
@@ -3029,6 +3040,7 @@ exports[`Components SchemaView discriminator should correctly render discriminat
30293040
},
30303041
}
30313042
}
3043+
fieldParentsName={[]}
30323044
isLast={true}
30333045
key="type"
30343046
renderDiscriminatorSwitch={[Function]}

src/services/MenuBuilder.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class MenuBuilder {
2424
const hasAutogenerated = [...(spec.tags || [])].find(
2525
tag => tag?.name === schemaDefinitionsTagName,
2626
);
27-
console.log('hasAutogenerated', hasAutogenerated, schemaDefinitionsTagName);
27+
2828
if (!hasAutogenerated && schemaDefinitionsTagName) {
2929
tags.push({ name: schemaDefinitionsTagName });
3030
}
@@ -38,7 +38,7 @@ export class MenuBuilder {
3838
} else {
3939
items.push(...MenuBuilder.getTagsItems(parser, tagsMap, undefined, undefined, options));
4040
}
41-
console.log('items', items);
41+
4242
return items;
4343
}
4444

src/services/RedocNormalizedOptions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export interface RedocRawOptions {
7070
hideFab?: boolean;
7171
minCharacterLengthToInitSearch?: number;
7272
showWebhookVerb?: boolean;
73+
hidePropertiesPrefix?: boolean;
7374
}
7475

7576
export function argValueToBoolean(val?: string | boolean, defaultValue?: boolean): boolean {
@@ -273,6 +274,7 @@ export class RedocNormalizedOptions {
273274
hideFab: boolean;
274275
minCharacterLengthToInitSearch: number;
275276
showWebhookVerb: boolean;
277+
hidePropertiesPrefix?: boolean;
276278

277279
nonce?: string;
278280

@@ -358,5 +360,6 @@ export class RedocNormalizedOptions {
358360
this.hideFab = argValueToBoolean(raw.hideFab);
359361
this.minCharacterLengthToInitSearch = argValueToNumber(raw.minCharacterLengthToInitSearch) || 3;
360362
this.showWebhookVerb = argValueToBoolean(raw.showWebhookVerb);
363+
this.hidePropertiesPrefix = argValueToBoolean(raw.hidePropertiesPrefix, true);
361364
}
362365
}

0 commit comments

Comments
 (0)