Skip to content
This repository has been archived by the owner on Oct 4, 2023. It is now read-only.

Commit

Permalink
fix: ignore the code-samples extension if placed at the root level (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored Jan 21, 2022
1 parent 502a11a commit 200ada9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
17 changes: 17 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ describe('#getExtension', () => {

expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).toHaveLength(5);
});

it('should not pick up the `code-samples` extension', () => {
const oas = Oas.init({
...petstore,
'x-readme': {
[extensions.CODE_SAMPLES]: [
{
name: 'Custom cURL snippet',
language: 'curl',
code: 'curl -X POST https://api.example.com/v2/alert',
},
],
},
});

expect(extensions.getExtension(extensions.CODE_SAMPLES, oas)).toBeUndefined();
});
});

describe('operation-level', () => {
Expand Down
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ export function getExtension(extension: keyof Extensions, oas: Oas, operation?:
}
}

// Because our `code-samples` extension is intended for operation-level use, if it's instead placed at the OAS-level
// we should ignore it.
if (extension === CODE_SAMPLES) {
return defaults[extension];
}

if (oas.hasExtension('x-readme')) {
const data = oas.getExtension('x-readme') as Extensions;
if (data && typeof data === 'object' && extension in data) {
Expand Down

0 comments on commit 200ada9

Please sign in to comment.