Skip to content

Commit 561dda7

Browse files
committed
Update models
1 parent 671fbdb commit 561dda7

File tree

1 file changed

+57
-21
lines changed

1 file changed

+57
-21
lines changed
Lines changed: 57 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import clsx from 'clsx';
22

3+
import type { OpenAPIV3 } from '@gitbook/openapi-parser';
34
import { OpenAPIDisclosureGroup } from '../OpenAPIDisclosureGroup';
45
import { OpenAPIRootSchema } from '../OpenAPISchema';
56
import { Section, SectionBody } from '../StaticSection';
@@ -22,29 +23,64 @@ export function OpenAPIModels(props: {
2223
blockKey: context.blockKey,
2324
};
2425

26+
if (!models.length) {
27+
return null;
28+
}
29+
2530
return (
2631
<div className={clsx('openapi-models', className)}>
27-
{models.length ? (
28-
<OpenAPIDisclosureGroup
29-
allowsMultipleExpanded
30-
icon={context.icons.chevronRight}
31-
groups={models.map(([name, schema]) => ({
32-
id: name,
33-
label: (
34-
<div className="openapi-response-tab-content" key={`model-${name}`}>
35-
<span className="openapi-response-statuscode">{name}</span>
36-
</div>
37-
),
38-
body: (
39-
<Section className="openapi-section-models">
40-
<SectionBody>
41-
<OpenAPIRootSchema schema={schema} context={clientContext} />
42-
</SectionBody>
43-
</Section>
44-
),
45-
}))}
46-
/>
47-
) : null}
32+
<OpenAPIRootModelsSchema models={models} context={clientContext} />
4833
</div>
4934
);
5035
}
36+
37+
/**
38+
* Root schema for OpenAPI models.
39+
* It displays a single model or a disclosure group for multiple models.
40+
*/
41+
function OpenAPIRootModelsSchema(props: {
42+
models: [string, OpenAPIV3.SchemaObject][];
43+
context: OpenAPIClientContext;
44+
}) {
45+
const { models, context } = props;
46+
47+
// If there is only one model, we show it directly.
48+
if (models.length === 1) {
49+
const schema = models?.[0]?.[1];
50+
51+
if (!schema) {
52+
return null;
53+
}
54+
55+
return (
56+
<Section>
57+
<SectionBody>
58+
<OpenAPIRootSchema schema={schema} context={context} />
59+
</SectionBody>
60+
</Section>
61+
);
62+
}
63+
64+
// If there are multiple models, we use a disclosure group to show them all.
65+
return (
66+
<OpenAPIDisclosureGroup
67+
allowsMultipleExpanded
68+
icon={context.icons.chevronRight}
69+
groups={models.map(([name, schema]) => ({
70+
id: name,
71+
label: (
72+
<div className="openapi-response-tab-content" key={`model-${name}`}>
73+
<span className="openapi-response-statuscode">{name}</span>
74+
</div>
75+
),
76+
body: (
77+
<Section className="openapi-section-models">
78+
<SectionBody>
79+
<OpenAPIRootSchema schema={schema} context={context} />
80+
</SectionBody>
81+
</Section>
82+
),
83+
}))}
84+
/>
85+
);
86+
}

0 commit comments

Comments
 (0)