Handle invalid OpenAPI Responses (#3223)

This commit is contained in:
Nolann B.
2025-05-08 11:54:46 +02:00
committed by GitHub
parent 47f01edcb8
commit cb5598dc19
3 changed files with 46 additions and 39 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---
Handle invalid OpenAPI Responses
@@ -6,8 +6,8 @@ import { OpenAPIResponseExampleContent } from './OpenAPIResponseExampleContent';
import { type OpenAPIContext, getOpenAPIClientContext } from './context'; import { type OpenAPIContext, getOpenAPIClientContext } from './context';
import type { OpenAPIOperationData, OpenAPIWebhookData } from './types'; import type { OpenAPIOperationData, OpenAPIWebhookData } from './types';
import { getExampleFromReference, getExamples } from './util/example'; import { getExampleFromReference, getExamples } from './util/example';
import { createStateKey, getStatusCodeDefaultLabel } from './utils'; import { createStateKey, getStatusCodeDefaultLabel, resolveDescription } from './utils';
import { checkIsReference, resolveDescription } from './utils'; import { checkIsReference } from './utils';
/** /**
* Display an example of the response content. * Display an example of the response content.
@@ -41,7 +41,9 @@ export function OpenAPIResponseExample(props: {
return Number(a) - Number(b); return Number(a) - Number(b);
}); });
const tabs = responses.map(([key, responseObject]) => { const tabs = responses
.filter(([_, responseObject]) => responseObject && typeof responseObject === 'object')
.map(([key, responseObject]) => {
const description = resolveDescription(responseObject); const description = resolveDescription(responseObject);
const label = description ? ( const label = description ? (
<Markdown source={description} /> <Markdown source={description} />
@@ -20,8 +20,9 @@ export function OpenAPIResponses(props: {
}) { }) {
const { responses, context } = props; const { responses, context } = props;
const groups = Object.entries(responses).map( const groups = Object.entries(responses)
([statusCode, response]: [string, OpenAPIV3.ResponseObject]) => { .filter(([_, response]) => response && typeof response === 'object')
.map(([statusCode, response]: [string, OpenAPIV3.ResponseObject]) => {
const tabs = (() => { const tabs = (() => {
// If there is no content, but there are headers, we need to show the headers // If there is no content, but there are headers, we need to show the headers
if ( if (
@@ -83,8 +84,7 @@ export function OpenAPIResponses(props: {
), ),
tabs, tabs,
}; };
} });
);
const state = useResponseExamplesState(context.blockKey, groups[0]?.key); const state = useResponseExamplesState(context.blockKey, groups[0]?.key);