Skip to content

Commit 47eb945

Browse files
committed
Fix UI display
1 parent 46fb3b5 commit 47eb945

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

packages/react-openapi/src/OpenAPIResponseExample.tsx

Lines changed: 52 additions & 19 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,28 +250,43 @@ 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) {
248-
// @TODO normally we should use the name of the schema but we don't have it
249-
const root = mediaTypeObject.schema.xml?.name ?? 'object';
269+
if (mediaType === 'application/xml') {
270+
// @TODO normally we should use the name of the schema but we don't have it
271+
// fix it when we got the reference name
272+
const root = mediaTypeObject.schema.xml?.name ?? 'object';
273+
return [
274+
{
275+
key: 'default',
276+
example: {
277+
value: {
278+
[root]: generateSchemaExample(mediaTypeObject.schema, {
279+
xml: mediaType === 'application/xml',
280+
}),
281+
},
282+
},
283+
},
284+
];
285+
}
250286
return [
251287
{
252-
value: {
253-
[root]: generateSchemaExample(mediaTypeObject.schema, {
254-
xml: mediaType === 'application/xml',
255-
}),
256-
},
288+
key: 'default',
289+
example: { value: generateSchemaExample(mediaTypeObject.schema) },
257290
},
258291
];
259292
}

0 commit comments

Comments
 (0)