Skip to content

Commit cb5598d

Browse files
authored
Handle invalid OpenAPI Responses (#3223)
1 parent 47f01ed commit cb5598d

File tree

3 files changed

+45
-38
lines changed

3 files changed

+45
-38
lines changed

.changeset/smart-feet-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
---
4+
5+
Handle invalid OpenAPI Responses

packages/react-openapi/src/OpenAPIResponseExample.tsx

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import { OpenAPIResponseExampleContent } from './OpenAPIResponseExampleContent';
66
import { type OpenAPIContext, getOpenAPIClientContext } from './context';
77
import type { OpenAPIOperationData, OpenAPIWebhookData } from './types';
88
import { getExampleFromReference, getExamples } from './util/example';
9-
import { createStateKey, getStatusCodeDefaultLabel } from './utils';
10-
import { checkIsReference, resolveDescription } from './utils';
9+
import { createStateKey, getStatusCodeDefaultLabel, resolveDescription } from './utils';
10+
import { checkIsReference } from './utils';
1111

1212
/**
1313
* Display an example of the response content.
@@ -41,45 +41,47 @@ export function OpenAPIResponseExample(props: {
4141
return Number(a) - Number(b);
4242
});
4343

44-
const tabs = responses.map(([key, responseObject]) => {
45-
const description = resolveDescription(responseObject);
46-
const label = description ? (
47-
<Markdown source={description} />
48-
) : (
49-
getStatusCodeDefaultLabel(key, context)
50-
);
44+
const tabs = responses
45+
.filter(([_, responseObject]) => responseObject && typeof responseObject === 'object')
46+
.map(([key, responseObject]) => {
47+
const description = resolveDescription(responseObject);
48+
const label = description ? (
49+
<Markdown source={description} />
50+
) : (
51+
getStatusCodeDefaultLabel(key, context)
52+
);
5153

52-
if (checkIsReference(responseObject)) {
53-
return {
54-
key: key,
55-
label,
56-
statusCode: key,
57-
body: (
58-
<OpenAPIExample
59-
example={getExampleFromReference(responseObject, context)}
60-
context={context}
61-
syntax="json"
62-
/>
63-
),
64-
};
65-
}
54+
if (checkIsReference(responseObject)) {
55+
return {
56+
key: key,
57+
label,
58+
statusCode: key,
59+
body: (
60+
<OpenAPIExample
61+
example={getExampleFromReference(responseObject, context)}
62+
context={context}
63+
syntax="json"
64+
/>
65+
),
66+
};
67+
}
68+
69+
if (!responseObject.content || Object.keys(responseObject.content).length === 0) {
70+
return {
71+
key: key,
72+
label,
73+
statusCode: key,
74+
body: <OpenAPIEmptyExample context={context} />,
75+
};
76+
}
6677

67-
if (!responseObject.content || Object.keys(responseObject.content).length === 0) {
6878
return {
6979
key: key,
7080
label,
7181
statusCode: key,
72-
body: <OpenAPIEmptyExample context={context} />,
82+
body: <OpenAPIResponse context={context} content={responseObject.content} />,
7383
};
74-
}
75-
76-
return {
77-
key: key,
78-
label,
79-
statusCode: key,
80-
body: <OpenAPIResponse context={context} content={responseObject.content} />,
81-
};
82-
});
84+
});
8385

8486
if (tabs.length === 0) {
8587
return null;

packages/react-openapi/src/OpenAPIResponses.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export function OpenAPIResponses(props: {
2020
}) {
2121
const { responses, context } = props;
2222

23-
const groups = Object.entries(responses).map(
24-
([statusCode, response]: [string, OpenAPIV3.ResponseObject]) => {
23+
const groups = Object.entries(responses)
24+
.filter(([_, response]) => response && typeof response === 'object')
25+
.map(([statusCode, response]: [string, OpenAPIV3.ResponseObject]) => {
2526
const tabs = (() => {
2627
// If there is no content, but there are headers, we need to show the headers
2728
if (
@@ -83,8 +84,7 @@ export function OpenAPIResponses(props: {
8384
),
8485
tabs,
8586
};
86-
}
87-
);
87+
});
8888

8989
const state = useResponseExamplesState(context.blockKey, groups[0]?.key);
9090

0 commit comments

Comments
 (0)