Skip to content

Commit

Permalink
Fix x-codeSamples: false not working at the single operation level (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse authored Jul 25, 2024
1 parent 709f1a1 commit ede2335
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/lemon-walls-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---

Fix x-codeSamples: false not working at the single operation level
8 changes: 6 additions & 2 deletions packages/react-openapi/src/OpenAPICodeSample.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/react-openapi/src/fetchOpenAPIOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];

/**
Expand Down

0 comments on commit ede2335

Please sign in to comment.