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

Commit

Permalink
feat: improved error handling when extensions aren't found
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Nov 20, 2021
1 parent 8d2d624 commit c49b368
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
27 changes: 27 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ test.each([
});

describe('#getExtension', () => {
it("should not throw an exception if `Oas` doesn't have an API definition", () => {
const oas = Oas.init(undefined);
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas)).toHaveLength(5);
});

it("should not throw an exception if `Operation` doesn't have an API definition", () => {
const oas = Oas.init(undefined);
const operation = oas.operation('/pet', 'post');
expect(extensions.getExtension(extensions.SAMPLES_LANGUAGES, oas, operation)).toHaveLength(5);
});

describe('oas-level extensions', () => {
it('should use the default extension value if the extension is not present', () => {
const oas = Oas.init(petstore);
Expand Down Expand Up @@ -43,6 +54,15 @@ describe('#getExtension', () => {
const oas = Oas.init({ ...petstore, [`x-${extensions.EXPLORER_ENABLED}`]: false });
expect(extensions.getExtension(extensions.EXPLORER_ENABLED, oas)).toBe(false);
});

it('should not throw if `x-readme` is not an object', () => {
const oas = Oas.init({
...petstore,
'x-readme': true,
});

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

describe('operation-level', () => {
Expand Down Expand Up @@ -75,6 +95,13 @@ describe('#getExtension', () => {

expect(extensions.getExtension(extensions.EXPLORER_ENABLED, oas, operation)).toBe(false);
});

it('should not throw if `x-readme` is not an object', () => {
const operation = oas.operation('/pet', 'post');
operation.schema['x-readme'] = true;

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

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export function getExtension(extension: keyof Extensions, oas: Oas, operation?:
if (operation) {
if (operation.hasExtension('x-readme')) {
const data = operation.getExtension('x-readme') as Extensions;
if (extension in data) {
if (data && typeof data === 'object' && extension in data) {
return data[extension];
}
}
Expand All @@ -68,7 +68,7 @@ export function getExtension(extension: keyof Extensions, oas: Oas, operation?:

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

0 comments on commit c49b368

Please sign in to comment.