Skip to content

Commit ba1f51d

Browse files
v50553490-cyberVladislav Abrosimov
andauthored
Examples rendering for 'multipart/form-data' (#1315)
Co-authored-by: Vladislav Abrosimov <Vladislav.Abrosimov@infowatch.com>
1 parent 6321cf5 commit ba1f51d

File tree

3 files changed

+115
-41
lines changed

3 files changed

+115
-41
lines changed

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FileArrayFormBodyItem/index.tsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ export default function FileArrayFormBodyItem({
4343
return;
4444
}
4545

46-
let maxIndex = 0;
47-
48-
newItems.keys().forEach((item) => {
49-
maxIndex = item > maxIndex ? item : maxIndex;
50-
});
5146
newItems.set(index, {
5247
src: `/path/to/${file.name}`,
5348
content: file,

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/FormBodyItem/index.tsx

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
* ========================================================================== */
77

8-
import React from "react";
8+
import React, { useEffect, useState } from "react";
99

1010
import FormFileUpload from "@theme/ApiExplorer/FormFileUpload";
1111
import FormLabel from "@theme/ApiExplorer/FormLabel";
@@ -24,6 +24,7 @@ interface FormBodyItemProps {
2424
schema: SchemaObject;
2525
label?: string;
2626
required?: boolean;
27+
exampleValue?: SchemaObject["example"];
2728
}
2829

2930
export default function FormBodyItem({
@@ -32,8 +33,27 @@ export default function FormBodyItem({
3233
schema,
3334
label,
3435
required,
36+
exampleValue,
3537
}: FormBodyItemProps): React.JSX.Element {
3638
const dispatch = useTypedDispatch();
39+
const [value, setValue] = useState(() => {
40+
let initialValue = exampleValue ?? "";
41+
42+
if (schemaObject.type === "object" && exampleValue) {
43+
initialValue = JSON.stringify(exampleValue, null, 2);
44+
}
45+
46+
return initialValue;
47+
});
48+
49+
useEffect(() => {
50+
if (value) {
51+
dispatch(setStringFormBody({ key: id, value }));
52+
} else {
53+
dispatch(clearFormBodyKey(id));
54+
}
55+
// eslint-disable-next-line react-hooks/exhaustive-deps
56+
}, []);
3757

3858
if (
3959
schemaObject.type === "array" &&
@@ -73,27 +93,18 @@ export default function FormBodyItem({
7393
);
7494
}
7595

76-
if (
77-
schemaObject.type === "object" &&
78-
(schemaObject.example || schemaObject.examples)
79-
) {
80-
const objectExample = JSON.stringify(
81-
schemaObject.example ?? schemaObject.examples[0],
82-
null,
83-
2
84-
);
85-
96+
if (schemaObject.type === "object") {
8697
return (
87-
<>
88-
{label && <FormLabel label={label} required={required} />}
89-
<LiveApp
90-
action={(code: string) =>
91-
dispatch(setStringFormBody({ key: id, value: code }))
92-
}
93-
>
94-
{objectExample}
95-
</LiveApp>
96-
</>
98+
<>
99+
{label && <FormLabel label={label} required={required} />}
100+
<LiveApp
101+
action={(code: string) =>
102+
dispatch(setStringFormBody({ key: id, value: code }))
103+
}
104+
>
105+
{value}
106+
</LiveApp>
107+
</>
97108
);
98109
}
99110

@@ -105,9 +116,11 @@ export default function FormBodyItem({
105116
<FormSelect
106117
label={label}
107118
required={required}
119+
value={value}
108120
options={["---", ...schemaObject.enum]}
109121
onChange={(e: React.ChangeEvent<HTMLSelectElement>) => {
110122
const val = e.target.value;
123+
setValue(val);
111124
if (val === "---") {
112125
dispatch(clearFormBodyKey(id));
113126
} else {
@@ -127,12 +140,14 @@ export default function FormBodyItem({
127140
<FormTextInput
128141
label={label}
129142
required={required}
143+
value={value}
130144
paramName={id}
131145
isRequired={
132146
Array.isArray(schema.required) && schema.required.includes(id)
133147
}
134148
placeholder={schemaObject.description || id}
135149
onChange={(e: React.ChangeEvent<HTMLInputElement>) => {
150+
setValue(e.target.value);
136151
dispatch(setStringFormBody({ key: id, value: e.target.value }));
137152
}}
138153
/>

packages/docusaurus-theme-openapi-docs/src/theme/ApiExplorer/Body/index.tsx

Lines changed: 79 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -316,22 +316,86 @@ function Body({
316316
) {
317317
return (
318318
<FormItem className="openapi-explorer__form-item-body-container">
319-
{Object.entries(schema.properties ?? {}).map(([key, val]: any) => {
320-
return (
321-
<FormItem key={key}>
322-
<FormBodyItem
323-
schemaObject={val}
324-
id={key}
325-
schema={schema}
326-
label={key}
327-
required={
328-
Array.isArray(schema.required) &&
329-
schema.required.includes(key)
319+
<SchemaTabs className="openapi-tabs__schema" lazy>
320+
{/* @ts-ignore */}
321+
<TabItem
322+
label={translate({
323+
id: OPENAPI_BODY.EXAMPLE_FROM_SCHEMA,
324+
message: "Example (from schema)",
325+
})}
326+
value="Example (from schema)"
327+
default
328+
>
329+
{Object.entries(schema.properties ?? {}).map(([key, val]: any) => {
330+
return (
331+
<FormItem key={key}>
332+
<FormBodyItem
333+
schemaObject={val}
334+
id={key}
335+
schema={schema}
336+
exampleValue={val.example}
337+
label={key}
338+
required={
339+
Array.isArray(schema.required) &&
340+
schema.required.includes(key)
341+
}
342+
></FormBodyItem>
343+
</FormItem>
344+
);
345+
})}
346+
</TabItem>
347+
{example && (
348+
// @ts-ignore
349+
<TabItem label="Example" value="example">
350+
{Object.entries(schema.properties ?? {}).map(
351+
([schemaKey, schemaVal]: any) => {
352+
return (
353+
<FormItem key={schemaKey}>
354+
<FormBodyItem
355+
schemaObject={schemaVal}
356+
id={schemaKey}
357+
schema={schema}
358+
exampleValue={example[schemaKey]}
359+
label={schemaKey}
360+
required={
361+
Array.isArray(schema.required) &&
362+
schema.required.includes(schemaKey)
363+
}
364+
></FormBodyItem>
365+
</FormItem>
366+
);
330367
}
331-
/>
332-
</FormItem>
333-
);
334-
})}
368+
)}
369+
</TabItem>
370+
)}
371+
{examples &&
372+
Object.entries(examples).map(([key, value]) => {
373+
return (
374+
// @ts-ignore
375+
<TabItem label={key} value={key} key={key}>
376+
{Object.entries(schema.properties ?? {}).map(
377+
([schemaKey, schemaVal]: any) => {
378+
return (
379+
<FormItem key={schemaKey}>
380+
<FormBodyItem
381+
schemaObject={schemaVal}
382+
id={schemaKey}
383+
schema={schema}
384+
exampleValue={value.value?.[schemaKey]}
385+
label={schemaKey}
386+
required={
387+
Array.isArray(schema.required) &&
388+
schema.required.includes(schemaKey)
389+
}
390+
></FormBodyItem>
391+
</FormItem>
392+
);
393+
}
394+
)}
395+
</TabItem>
396+
);
397+
})}
398+
</SchemaTabs>
335399
</FormItem>
336400
);
337401
}

0 commit comments

Comments
 (0)