Skip to content

Commit ede2335

Browse files
authored
Fix x-codeSamples: false not working at the single operation level (#2408)
1 parent 709f1a1 commit ede2335

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

.changeset/lemon-walls-eat.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 x-codeSamples: false not working at the single operation level

packages/react-openapi/src/OpenAPICodeSample.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export function OpenAPICodeSample(props: {
5252
}> = null;
5353
(['x-custom-examples', 'x-code-samples', 'x-codeSamples'] as const).forEach((key) => {
5454
const customSamples = data.operation[key];
55-
if (customSamples) {
55+
if (customSamples && Array.isArray(customSamples)) {
5656
customCodeSamples = customSamples.map((sample) => ({
5757
key: `redocly-${sample.lang}`,
5858
label: sample.label,
@@ -61,7 +61,11 @@ export function OpenAPICodeSample(props: {
6161
}
6262
});
6363

64-
const samples = customCodeSamples ?? (data['x-codeSamples'] !== false ? autoCodeSamples : []);
64+
// Code samples can be disabled at the top-level or at the operation level
65+
// If code samples are defined at the operation level, it will override the top-level setting
66+
const codeSamplesDisabled =
67+
data['x-codeSamples'] === false || data.operation['x-codeSamples'] === false;
68+
const samples = customCodeSamples ?? (!codeSamplesDisabled ? autoCodeSamples : []);
6569
if (samples.length === 0) {
6670
return null;
6771
}

packages/react-openapi/src/fetchOpenAPIOperation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export interface OpenAPICustomSpecProperties {
4343
*/
4444
export interface OpenAPICustomOperationProperties {
4545
'x-code-samples'?: OpenAPICustomCodeSample[];
46-
'x-codeSamples'?: OpenAPICustomCodeSample[];
46+
'x-codeSamples'?: OpenAPICustomCodeSample[] | false;
4747
'x-custom-examples'?: OpenAPICustomCodeSample[];
4848

4949
/**

0 commit comments

Comments
 (0)