Skip to content

Commit 237b703

Browse files
authored
Fix crash in OpenAPI block because of undefined examples (#2458)
1 parent cf3045a commit 237b703

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

.changeset/silent-donuts-love.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+
Fix crash when `example` is undefined for a response

packages/react-openapi/src/OpenAPICodeSample.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,19 @@ export function OpenAPICodeSample(props: {
6969
(['x-custom-examples', 'x-code-samples', 'x-codeSamples'] as const).forEach((key) => {
7070
const customSamples = data.operation[key];
7171
if (customSamples && Array.isArray(customSamples)) {
72-
customCodeSamples = customSamples.map((sample) => ({
73-
key: `redocly-${sample.lang}`,
74-
label: sample.label,
75-
body: <context.CodeBlock code={sample.source} syntax={sample.lang} />,
76-
}));
72+
customCodeSamples = customSamples
73+
.filter((sample) => {
74+
return (
75+
typeof sample.label === 'string' &&
76+
typeof sample.source === 'string' &&
77+
typeof sample.lang === 'string'
78+
);
79+
})
80+
.map((sample) => ({
81+
key: `redocly-${sample.lang}`,
82+
label: sample.label,
83+
body: <context.CodeBlock code={sample.source} syntax={sample.lang} />,
84+
}));
7785
}
7886
});
7987

packages/react-openapi/src/OpenAPIResponseExample.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export function OpenAPIResponseExample(props: {
5353
}
5454

5555
const example = generateSchemaExample(schema);
56+
if (example === undefined) {
57+
return null;
58+
}
59+
5660
return {
5761
key: `${response[0]}`,
5862
label: `${response[0]}`,

0 commit comments

Comments
 (0)