Fix crash in OpenAPI block because of undefined examples (#2458)

This commit is contained in:
Samy Pessé
2024-09-06 16:55:04 +02:00
committed by GitHub
parent cf3045a718
commit 237b703a6c
3 changed files with 22 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---
Fix crash when `example` is undefined for a response
@@ -69,11 +69,19 @@ export function OpenAPICodeSample(props: {
(['x-custom-examples', 'x-code-samples', 'x-codeSamples'] as const).forEach((key) => {
const customSamples = data.operation[key];
if (customSamples && Array.isArray(customSamples)) {
customCodeSamples = customSamples.map((sample) => ({
key: `redocly-${sample.lang}`,
label: sample.label,
body: <context.CodeBlock code={sample.source} syntax={sample.lang} />,
}));
customCodeSamples = customSamples
.filter((sample) => {
return (
typeof sample.label === 'string' &&
typeof sample.source === 'string' &&
typeof sample.lang === 'string'
);
})
.map((sample) => ({
key: `redocly-${sample.lang}`,
label: sample.label,
body: <context.CodeBlock code={sample.source} syntax={sample.lang} />,
}));
}
});
@@ -53,6 +53,10 @@ export function OpenAPIResponseExample(props: {
}
const example = generateSchemaExample(schema);
if (example === undefined) {
return null;
}
return {
key: `${response[0]}`,
label: `${response[0]}`,