Compare commits

...

2 Commits

Author SHA1 Message Date
Nolann Biron 50a3b2dea1 Changeset 2025-02-19 14:03:31 +01:00
Nolann Biron 17ee7bdebf Handle all type of content type in examples 2025-02-19 13:32:02 +01:00
2 changed files with 18 additions and 8 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---
Display OpenAPI response examples in multiple languages
@@ -46,13 +46,10 @@ export function OpenAPIResponseExample(props: {
return null; return null;
} }
const key = Object.keys(responseObject.content)[0]; const key = Object.keys(responseObject.content)[0];
return ( return [key, key ? responseObject.content[key] : null];
responseObject.content['application/json'] ??
(key ? responseObject.content[key] : null)
);
})(); })();
if (!mediaTypeObject) { if (!mediaTypeObject || !mediaTypeObject[1]) {
return { return {
key: key, key: key,
label: key, label: key,
@@ -63,7 +60,7 @@ export function OpenAPIResponseExample(props: {
const example = handleUnresolvedReference( const example = handleUnresolvedReference(
(() => { (() => {
const { examples, example } = mediaTypeObject; const { examples, example } = mediaTypeObject[1];
if (examples) { if (examples) {
const key = Object.keys(examples)[0]; const key = Object.keys(examples)[0];
if (key) { if (key) {
@@ -79,7 +76,7 @@ export function OpenAPIResponseExample(props: {
return { value: example }; return { value: example };
} }
const schema = mediaTypeObject.schema; const schema = mediaTypeObject[1].schema;
if (!schema) { if (!schema) {
return null; return null;
} }
@@ -99,7 +96,7 @@ export function OpenAPIResponseExample(props: {
? example.value ? example.value
: stringifyOpenAPI(example.value, null, 2) : stringifyOpenAPI(example.value, null, 2)
} }
syntax="json" syntax={fetchSyntaxFromContentType(mediaTypeObject[0])}
/> />
) : ( ) : (
<OpenAPIEmptyResponseExample /> <OpenAPIEmptyResponseExample />
@@ -143,3 +140,11 @@ function handleUnresolvedReference(
return input; return input;
} }
function fetchSyntaxFromContentType(contentType: string) {
if (contentType.includes('json')) {
return 'json';
}
return contentType.split('/')[1] || 'json';
}