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

fix: ignore the code-samples extension if placed at the root level #40

Merged
merged 1 commit into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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