-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(helpers): reorganize OpenAPI predicates into separate module
Refs #2717
- Loading branch information
Showing
5 changed files
with
137 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export const isOpenAPI2 = (spec) => { | ||
try { | ||
const { swagger } = spec; | ||
return swagger === '2.0'; | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
export const isOpenAPI30 = (spec) => { | ||
try { | ||
const { openapi } = spec; | ||
return typeof openapi === 'string' && openapi.startsWith('3.0'); | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export const isOpenAPI31 = (spec) => { | ||
try { | ||
const { openapi } = spec; | ||
return typeof openapi === 'string' && openapi.startsWith('3.1'); | ||
} catch { | ||
return false; | ||
} | ||
}; | ||
|
||
export const isOpenAPI3 = (spec) => isOpenAPI30(spec) || isOpenAPI31(spec); | ||
|
||
// backward compatibility export | ||
export { isOpenAPI2 as isSwagger2 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import { | ||
isOpenAPI30, | ||
isOpenAPI31, | ||
isOpenAPI3, | ||
isOpenAPI2, | ||
isSwagger2, | ||
} from '../../src/helpers/openapi-predicates.js'; | ||
|
||
describe('helpers - OpenAPI predicates', () => { | ||
describe('isOpenAPI30', () => { | ||
test('should detect OpenAPI 3.0.x versions', () => { | ||
expect(isOpenAPI30({ openapi: '3.0.0' })).toBe(true); | ||
expect(isOpenAPI30({ openapi: '3.0.1' })).toBe(true); | ||
expect(isOpenAPI30({ openapi: '3.0.2' })).toBe(true); | ||
expect(isOpenAPI30({ openapi: '3.0.3' })).toBe(true); | ||
}); | ||
|
||
test('should reject other OpenAPI versions', () => { | ||
expect(isOpenAPI30({ openapi: '3.1.0' })).toBe(false); | ||
expect(isOpenAPI30({ swagger: '2.0' })).toBe(false); | ||
}); | ||
|
||
test('should reject values that are not OpenAPI spec', () => { | ||
expect(isOpenAPI30(null)).toBe(false); | ||
expect(isOpenAPI30(undefined)).toBe(false); | ||
expect(isOpenAPI30({})).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isOpenAPI31', () => { | ||
test('should detect OpenAPI 3.1.x versions', () => { | ||
expect(isOpenAPI31({ openapi: '3.1.0' })).toBe(true); | ||
expect(isOpenAPI31({ openapi: '3.1.1' })).toBe(true); | ||
}); | ||
|
||
test('should reject other OpenAPI versions', () => { | ||
expect(isOpenAPI31({ openapi: '3.0.0' })).toBe(false); | ||
expect(isOpenAPI31({ swagger: '2.0' })).toBe(false); | ||
}); | ||
|
||
test('should reject values that are not OpenAPI spec', () => { | ||
expect(isOpenAPI31(null)).toBe(false); | ||
expect(isOpenAPI31(undefined)).toBe(false); | ||
expect(isOpenAPI31({})).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isOpenAPI3', () => { | ||
test('should detect OpenAPI 3.x.y versions', () => { | ||
expect(isOpenAPI3({ openapi: '3.0.0' })).toBe(true); | ||
expect(isOpenAPI3({ openapi: '3.0.1' })).toBe(true); | ||
expect(isOpenAPI3({ openapi: '3.0.2' })).toBe(true); | ||
expect(isOpenAPI3({ openapi: '3.0.3' })).toBe(true); | ||
expect(isOpenAPI3({ openapi: '3.1.0' })).toBe(true); | ||
expect(isOpenAPI3({ openapi: '3.1.1' })).toBe(true); | ||
}); | ||
|
||
test('should reject other OpenAPI versions', () => { | ||
expect(isOpenAPI3({ openapi: '3.2.0' })).toBe(false); | ||
expect(isOpenAPI3({ swagger: '2.0' })).toBe(false); | ||
}); | ||
|
||
test('should reject values that are not OpenAPI spec', () => { | ||
expect(isOpenAPI3(null)).toBe(false); | ||
expect(isOpenAPI3(undefined)).toBe(false); | ||
expect(isOpenAPI3({})).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('isOpenAPI2', () => { | ||
test('should detect OpenAPI 2.0 versions', () => { | ||
expect(isOpenAPI2({ swagger: '2.0' })).toBe(true); | ||
}); | ||
|
||
test('should reject other OpenAPI versions', () => { | ||
expect(isOpenAPI2({ openapi: '3.0.0' })).toBe(false); | ||
expect(isOpenAPI2({ openapi: '3.1.0' })).toBe(false); | ||
}); | ||
|
||
test('should reject values that are not OpenAPI spec', () => { | ||
expect(isOpenAPI2(null)).toBe(false); | ||
expect(isOpenAPI2(undefined)).toBe(false); | ||
expect(isOpenAPI2({})).toBe(false); | ||
}); | ||
|
||
describe('should be aliased by isSwagger2', () => { | ||
test('should detect OpenAPI 2.0 versions', () => { | ||
expect(isSwagger2({ swagger: '2.0' })).toBe(true); | ||
}); | ||
|
||
test('should reject other OpenAPI versions', () => { | ||
expect(isSwagger2({ openapi: '3.0.0' })).toBe(false); | ||
expect(isSwagger2({ openapi: '3.1.0' })).toBe(false); | ||
}); | ||
|
||
test('should reject values that are not OpenAPI spec', () => { | ||
expect(isSwagger2(null)).toBe(false); | ||
expect(isSwagger2(undefined)).toBe(false); | ||
expect(isSwagger2({})).toBe(false); | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.