diff --git a/__tests__/index.test.ts b/__tests__/index.test.ts index 853dc8c..1661485 100644 --- a/__tests__/index.test.ts +++ b/__tests__/index.test.ts @@ -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); @@ -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', () => { @@ -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); + }); }); }); diff --git a/package-lock.json b/package-lock.json index 0386902..07f7699 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11769,9 +11769,9 @@ "dev": true }, "node_modules/oas": { - "version": "17.1.0", - "resolved": "https://registry.npmjs.org/oas/-/oas-17.1.0.tgz", - "integrity": "sha512-iaikb9s+xUi9AYqhak9nxX6H4lWUBEZ9FNQVzNb/N/lnD3Ue4dVYZ0ykMbdA+Rk0CK747RUJR4GH3j0HGJNIQQ==", + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/oas/-/oas-17.1.3.tgz", + "integrity": "sha512-2N2Br8WbujxBCSFaNk10oRaYGM8ZTQAN6D4VMA/qAfpAbRWi+N3RdHsivjO9n2CX4XUwDjkqfzVy0RK/vVergw==", "peer": true, "dependencies": { "@apidevtools/json-schema-ref-parser": "^9.0.6", @@ -23151,9 +23151,9 @@ "dev": true }, "oas": { - "version": "17.1.0", - "resolved": "https://registry.npmjs.org/oas/-/oas-17.1.0.tgz", - "integrity": "sha512-iaikb9s+xUi9AYqhak9nxX6H4lWUBEZ9FNQVzNb/N/lnD3Ue4dVYZ0ykMbdA+Rk0CK747RUJR4GH3j0HGJNIQQ==", + "version": "17.1.3", + "resolved": "https://registry.npmjs.org/oas/-/oas-17.1.3.tgz", + "integrity": "sha512-2N2Br8WbujxBCSFaNk10oRaYGM8ZTQAN6D4VMA/qAfpAbRWi+N3RdHsivjO9n2CX4XUwDjkqfzVy0RK/vVergw==", "peer": true, "requires": { "@apidevtools/json-schema-ref-parser": "^9.0.6", diff --git a/src/index.ts b/src/index.ts index e9e4604..86f2432 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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]; } } @@ -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]; } }