Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 7ec69b4

Browse files
fix: fixed schema header and body structure for oneOf, patternProperties, button array item types
1 parent 6851aea commit 7ec69b4

File tree

7 files changed

+194
-6
lines changed

7 files changed

+194
-6
lines changed

src/features/Apiexplorer/Schema/RecursiveContent/RecursiveProperties/index.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React from 'react';
22
import SchemaDescription from '../SchemaDescription';
33
import SchemaObjectContent from '../SchemaObjectContent';
44
import StreamTypesObject from '../StreamTypesObject';
5+
import SchemaOneOfObjectContent from '../SchemaOneOfObjectContent';
56

67
type TRecursiveProperties = {
78
is_open: boolean;
@@ -38,14 +39,20 @@ const RecursiveProperties = ({ is_open, properties, value, jsonSchema }: TRecurs
3839
{index === 0 && value?.items?.description && (
3940
<SchemaDescription description={value.items.description} />
4041
)}
41-
{key === 'forget_all' && 'oneOf' in value[key] ? (
42+
{key === 'forget_all' && 'oneOf' in properties[key] ? (
4243
<SchemaObjectContent
4344
key={key}
4445
key_value={key}
4546
properties={properties}
4647
jsonSchema={jsonSchema}
4748
is_stream_types
4849
/>
50+
) : typeof properties[key] === 'object' && 'oneOf' in properties[key] ? (
51+
<SchemaOneOfObjectContent
52+
property={properties[key]}
53+
key_title={key}
54+
jsonSchema={jsonSchema}
55+
/>
4956
) : (
5057
<SchemaObjectContent key={key} key_value={key} properties={properties} />
5158
)}

src/features/Apiexplorer/Schema/RecursiveContent/SchemaBodyHeader/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ type TSchemaBodyHeader = {
1313
examples: string[];
1414
enum;
1515
is_stream_types: boolean;
16+
items_type: string;
1617
};
1718

1819
const SchemaBodyHeader = ({
@@ -26,6 +27,7 @@ const SchemaBodyHeader = ({
2627
is_open_object,
2728
setIsOpenObject,
2829
is_stream_types,
30+
items_type,
2931
}: TSchemaBodyHeader) => {
3032
let typeClassName;
3133
switch (type) {
@@ -110,7 +112,7 @@ const SchemaBodyHeader = ({
110112
<div className={styles.schemaObjectContent}>
111113
<div>
112114
<button onClick={() => setIsOpenObject(!is_open_object)}>
113-
{title ? key_value : 'object'}
115+
{title ? key_value : items_type ? items_type : 'object'}
114116
</button>
115117
</div>
116118
</div>

src/features/Apiexplorer/Schema/RecursiveContent/SchemaObjectContent/index.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export default function SchemaObjectContent({
3030
examples,
3131
enum: _enum,
3232
title,
33+
items,
3334
} = properties[key_value];
3435
const value = properties[key_value];
3536
let data;
@@ -60,6 +61,7 @@ export default function SchemaObjectContent({
6061
is_open_object={is_open_object}
6162
setIsOpenObject={setIsOpenObject}
6263
is_stream_types={is_stream_types}
64+
items_type={items?.type}
6365
/>
6466
{/* Description */}
6567
<SchemaDescription description={description} />
@@ -74,7 +76,7 @@ export default function SchemaObjectContent({
7476
{!is_code_open && (
7577
<RecursiveProperties
7678
is_open={is_open_object}
77-
properties={value.properties || value?.items?.properties}
79+
properties={value.properties || value?.items?.properties || value.patternProperties}
7880
value={value}
7981
jsonSchema={jsonSchema}
8082
/>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React from 'react';
2+
import styles from '../../Schema.module.scss';
3+
4+
type TSchemaOneOfBodyHeader = {
5+
key_title: string;
6+
oneOf: Record<string, any>[];
7+
updateIndexArray;
8+
};
9+
10+
const SchemaOneOfBodyHeader = ({ key_title, oneOf, updateIndexArray }: TSchemaOneOfBodyHeader) => {
11+
let previous_pattern = '';
12+
const generateClassName = (type) => {
13+
let typeClass;
14+
switch (type) {
15+
case 'number':
16+
typeClass = styles.number;
17+
break;
18+
case 'array':
19+
typeClass = styles.array;
20+
break;
21+
case 'integer':
22+
typeClass = styles.integer;
23+
break;
24+
case 'null':
25+
typeClass = styles.null;
26+
break;
27+
default:
28+
typeClass = styles.string;
29+
break;
30+
}
31+
return typeClass;
32+
};
33+
return (
34+
<div className={styles.schemaBodyHeader}>
35+
<div className={styles.schemaBodyType}>
36+
<div className={styles.enumFlex}>
37+
<p style={{ fontSize: '2.6rem' }}>
38+
<strong>{key_title}</strong>
39+
</p>
40+
<div className={styles.enumContainer}>
41+
<span className={styles.enumLabel}>one of</span>
42+
{oneOf &&
43+
Array.isArray(oneOf) &&
44+
oneOf.map((object, index) => {
45+
const show_btn = object?.description || object?.properties;
46+
const typeClassName = generateClassName(object?.type);
47+
if (
48+
index !== 0 &&
49+
(object?.pattern || object?.item?.pattern) !== previous_pattern
50+
) {
51+
previous_pattern = object.pattern;
52+
}
53+
54+
return (
55+
<React.Fragment key={index}>
56+
<div className={styles.schemaObjectContent}>
57+
{show_btn ? (
58+
<button
59+
onClick={() => {
60+
updateIndexArray(index);
61+
}}
62+
>
63+
{object.title ? object.title : object.type}
64+
</button>
65+
) : (
66+
<span className={`${styles.enumType} ${typeClassName}`}>{object.type}</span>
67+
)}
68+
{'pattern' in object && (
69+
<div className={styles.schemaRegexContainer}>
70+
{previous_pattern !== object.pattern && (
71+
<span className={styles.schemaBodyPattern}>{object.pattern}</span>
72+
)}
73+
</div>
74+
)}
75+
</div>
76+
</React.Fragment>
77+
);
78+
})}
79+
</div>
80+
</div>
81+
</div>
82+
</div>
83+
);
84+
};
85+
86+
export default SchemaOneOfBodyHeader;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React, { Suspense, useState } from 'react';
2+
import RecursiveProperties from '../RecursiveProperties';
3+
import SchemaDescription from '../SchemaDescription';
4+
import SourceButton from '../../SourceButton/SourceButton';
5+
import styles from '../../Schema.module.scss';
6+
import clsx from 'clsx';
7+
import SchemaOneOfBodyHeader from '../SchemaOneOfBodyHeader';
8+
9+
const ReactJson = React.lazy(() => import('react-json-view'));
10+
11+
type TSchemaOneOfObjectContent = {
12+
property: any;
13+
key_title: string;
14+
jsonSchema: any;
15+
};
16+
17+
export default function SchemaOneOfObjectContent({
18+
property,
19+
key_title,
20+
jsonSchema,
21+
}: TSchemaOneOfObjectContent) {
22+
const { description, oneOf } = property;
23+
const [is_code_open, setIsCodeOpen] = useState<boolean>(false);
24+
const [index_arr, setIndexArr] = useState(Array(property.length).fill(false));
25+
26+
const schema = property;
27+
let data;
28+
try {
29+
data = JSON.stringify(schema, null, 2);
30+
} catch (error) {
31+
data = '';
32+
console.error('There was an issue stringifying JSON data: ', error);
33+
}
34+
React.useEffect(() => {
35+
setIsCodeOpen(false);
36+
setIndexArr([]);
37+
}, [property]);
38+
39+
const updateIndexArray = (index: number) => {
40+
setIndexArr((prevArray) => {
41+
const newArray = [...prevArray];
42+
newArray[index] = !newArray[index];
43+
return newArray;
44+
});
45+
};
46+
47+
return (
48+
<Suspense fallback={<div>Loading...</div>}>
49+
<div className={styles.schemaBodySignature}>
50+
<SourceButton is_code_open={is_code_open} setIsCodeOpen={setIsCodeOpen} />
51+
52+
{/* Header */}
53+
<SchemaOneOfBodyHeader
54+
key_title={key_title}
55+
oneOf={oneOf}
56+
updateIndexArray={updateIndexArray}
57+
/>
58+
{/* Description */}
59+
<SchemaDescription description={description} />
60+
61+
{is_code_open && (
62+
<div className={styles.reactJsonView}>
63+
<ReactJson src={JSON.parse(data)} theme='tube' />
64+
</div>
65+
)}
66+
67+
{!is_code_open && (
68+
<>
69+
{index_arr.map(
70+
(val, index) =>
71+
val === true && (
72+
<RecursiveProperties
73+
is_open
74+
properties={oneOf[index]['properties']}
75+
value={oneOf[index]}
76+
jsonSchema={jsonSchema}
77+
key={index}
78+
/>
79+
),
80+
)}
81+
</>
82+
)}
83+
</div>
84+
</Suspense>
85+
);
86+
}

src/features/Apiexplorer/Schema/Schema.module.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
width: fit-content;
169169
display: flex;
170170
flex-direction: row;
171+
align-items: center;
171172

172173
.schemaBodyPattern {
173174
width: 100%;
@@ -262,6 +263,9 @@
262263
&.number {
263264
color: var(--schema-number);
264265
}
266+
&.null {
267+
color: var(--schema-null);
268+
}
265269
}
266270

267271
.enumLabel,

src/styles/index.scss

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@
3434
--schema-array: #ff8fc8;
3535
--schema-number: #acb2ff;
3636
--schema-integer: #f8c272;
37+
--schema-null: #ff444f;
3738
--spinner-height: 100px;
3839
--spinner-width: 100px;
3940
--smoke: #414652;
40-
--admin-text: #22BD41;
41-
--admin-border: #33C9517A;
41+
--admin-text: #22bd41;
42+
--admin-border: #33c9517a;
4243
}
4344

4445
/* For readability concerns, you should choose a lighter palette in dark mode. */
@@ -164,7 +165,7 @@ body {
164165
display: none !important;
165166
}
166167

167-
.unofficial-host {
168+
.unofficial-host {
168169
.hide-sidebar-item {
169170
display: none !important;
170171
}

0 commit comments

Comments
 (0)