-
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
95 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,18 @@ | ||
export const isOpenAPI2 = (spec) => { | ||
const { swagger } = spec; | ||
return swagger === '2.0'; | ||
}; | ||
export const isOpenAPI30 = (spec) => { | ||
const { openapi } = spec; | ||
return typeof openapi === 'string' && openapi.startsWith('3.0'); | ||
}; | ||
|
||
export const isOpenAPI31 = (spec) => { | ||
const { openapi } = spec; | ||
return typeof openapi === 'string' && openapi.startsWith('3.1'); | ||
}; | ||
|
||
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,73 @@ | ||
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); | ||
}); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
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); | ||
}); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.