Skip to content

Commit 29b2046

Browse files
committed
Fix UI display
1 parent 46fb3b5 commit 29b2046

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

packages/react-openapi/src/OpenAPIResponseExample.tsx

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,11 @@ function OpenAPIResponse(props: {
132132
});
133133

134134
return (
135-
<OpenAPITabs stateKey={createStateKey('media-type-examples')} items={tabs}>
136-
<InteractiveSection header={<OpenAPITabsList />} className="openapi-response-example">
135+
<OpenAPITabs stateKey={createStateKey('response-media-types')} items={tabs}>
136+
<InteractiveSection
137+
header={<OpenAPITabsList />}
138+
className="openapi-response-media-types"
139+
>
137140
<OpenAPITabsPanels />
138141
</InteractiveSection>
139142
</OpenAPITabs>
@@ -155,20 +158,35 @@ function OpenAPIResponseMediaType(props: {
155158
}
156159

157160
if (examples.length === 1) {
158-
return <OpenAPIExample example={firstExample} context={props.context} syntax={syntax} />;
161+
return (
162+
<OpenAPIExample
163+
example={firstExample.example}
164+
context={props.context}
165+
syntax={syntax}
166+
/>
167+
);
159168
}
160169

161170
const tabs = examples.map((example) => {
162171
return {
163-
key: example.summary || 'Example',
164-
label: example.summary || 'Example',
165-
body: <OpenAPIExample example={firstExample} context={props.context} syntax={syntax} />,
172+
key: example.key,
173+
label: example.example.summary || example.key,
174+
body: (
175+
<OpenAPIExample
176+
example={firstExample.example}
177+
context={props.context}
178+
syntax={syntax}
179+
/>
180+
),
166181
};
167182
});
168183

169184
return (
170-
<OpenAPITabs stateKey={createStateKey('media-type-examples')} items={tabs}>
171-
<InteractiveSection header={<OpenAPITabsList />} className="openapi-response-example">
185+
<OpenAPITabs stateKey={createStateKey('response-media-type-examples')} items={tabs}>
186+
<InteractiveSection
187+
header={<OpenAPITabsList />}
188+
className="openapi-response-media-type-examples"
189+
>
172190
<OpenAPITabsPanels />
173191
</InteractiveSection>
174192
</OpenAPITabs>
@@ -232,27 +250,33 @@ function getSyntaxFromMediaType(mediaType: string): string {
232250
function getExamplesFromMediaTypeObject(args: {
233251
mediaType: string;
234252
mediaTypeObject: OpenAPIV3.MediaTypeObject;
235-
}): OpenAPIV3.ExampleObject[] {
253+
}): { key: string; example: OpenAPIV3.ExampleObject }[] {
236254
const { mediaTypeObject, mediaType } = args;
237255
if (mediaTypeObject.examples) {
238-
return Object.values(mediaTypeObject.examples).map((example) => {
239-
return checkIsReference(example) ? getExampleFromReference(example) : example;
256+
return Object.entries(mediaTypeObject.examples).map(([key, example]) => {
257+
return {
258+
key,
259+
example: checkIsReference(example) ? getExampleFromReference(example) : example,
260+
};
240261
});
241262
}
242263

243264
if (mediaTypeObject.example) {
244-
return [{ value: mediaTypeObject.example }];
265+
return [{ key: 'default', example: { value: mediaTypeObject.example } }];
245266
}
246267

247268
if (mediaTypeObject.schema) {
248269
// @TODO normally we should use the name of the schema but we don't have it
249270
const root = mediaTypeObject.schema.xml?.name ?? 'object';
250271
return [
251272
{
252-
value: {
253-
[root]: generateSchemaExample(mediaTypeObject.schema, {
254-
xml: mediaType === 'application/xml',
255-
}),
273+
key: 'default',
274+
example: {
275+
value: {
276+
[root]: generateSchemaExample(mediaTypeObject.schema, {
277+
xml: mediaType === 'application/xml',
278+
}),
279+
},
256280
},
257281
},
258282
];

0 commit comments

Comments
 (0)