From ede2335fb331bc7272b16e33c4d709a6364a81a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samy=20Pess=C3=A9?= Date: Thu, 25 Jul 2024 14:59:30 +0200 Subject: [PATCH] Fix x-codeSamples: false not working at the single operation level (#2408) --- .changeset/lemon-walls-eat.md | 5 +++++ packages/react-openapi/src/OpenAPICodeSample.tsx | 8 ++++++-- packages/react-openapi/src/fetchOpenAPIOperation.ts | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changeset/lemon-walls-eat.md diff --git a/.changeset/lemon-walls-eat.md b/.changeset/lemon-walls-eat.md new file mode 100644 index 000000000..046aad95f --- /dev/null +++ b/.changeset/lemon-walls-eat.md @@ -0,0 +1,5 @@ +--- +'@gitbook/react-openapi': patch +--- + +Fix x-codeSamples: false not working at the single operation level diff --git a/packages/react-openapi/src/OpenAPICodeSample.tsx b/packages/react-openapi/src/OpenAPICodeSample.tsx index 0cc4db1a5..c94289869 100644 --- a/packages/react-openapi/src/OpenAPICodeSample.tsx +++ b/packages/react-openapi/src/OpenAPICodeSample.tsx @@ -52,7 +52,7 @@ export function OpenAPICodeSample(props: { }> = null; (['x-custom-examples', 'x-code-samples', 'x-codeSamples'] as const).forEach((key) => { const customSamples = data.operation[key]; - if (customSamples) { + if (customSamples && Array.isArray(customSamples)) { customCodeSamples = customSamples.map((sample) => ({ key: `redocly-${sample.lang}`, label: sample.label, @@ -61,7 +61,11 @@ export function OpenAPICodeSample(props: { } }); - const samples = customCodeSamples ?? (data['x-codeSamples'] !== false ? autoCodeSamples : []); + // Code samples can be disabled at the top-level or at the operation level + // If code samples are defined at the operation level, it will override the top-level setting + const codeSamplesDisabled = + data['x-codeSamples'] === false || data.operation['x-codeSamples'] === false; + const samples = customCodeSamples ?? (!codeSamplesDisabled ? autoCodeSamples : []); if (samples.length === 0) { return null; } diff --git a/packages/react-openapi/src/fetchOpenAPIOperation.ts b/packages/react-openapi/src/fetchOpenAPIOperation.ts index c03c648d3..f6b23d841 100644 --- a/packages/react-openapi/src/fetchOpenAPIOperation.ts +++ b/packages/react-openapi/src/fetchOpenAPIOperation.ts @@ -43,7 +43,7 @@ export interface OpenAPICustomSpecProperties { */ export interface OpenAPICustomOperationProperties { 'x-code-samples'?: OpenAPICustomCodeSample[]; - 'x-codeSamples'?: OpenAPICustomCodeSample[]; + 'x-codeSamples'?: OpenAPICustomCodeSample[] | false; 'x-custom-examples'?: OpenAPICustomCodeSample[]; /**