-
-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from jordanshatford/feat/unify-v2-v3-parsing
feat: unify v2 v3 parsing
- Loading branch information
Showing
31 changed files
with
253 additions
and
250 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,101 @@ | ||
import { afterEach, describe, expect, it, vi } from 'vitest'; | ||
|
||
import { parse } from '..'; | ||
import * as parseV2 from '../v2'; | ||
import * as parseV3 from '../v3'; | ||
|
||
describe('parse', () => { | ||
afterEach(() => { | ||
vi.restoreAllMocks(); | ||
}); | ||
|
||
const options: Parameters<typeof parse>[1] = { | ||
client: 'fetch', | ||
enums: true, | ||
exportCore: true, | ||
exportModels: true, | ||
exportSchemas: true, | ||
exportServices: true, | ||
format: true, | ||
input: '', | ||
lint: false, | ||
operationId: true, | ||
output: '', | ||
postfixModels: '', | ||
postfixServices: '', | ||
serviceResponse: 'body', | ||
useDateType: false, | ||
useOptions: true, | ||
write: false, | ||
}; | ||
|
||
it('uses v2 parser', () => { | ||
const spy = vi.spyOn(parseV2, 'parse'); | ||
|
||
const spec: Parameters<typeof parse>[0] = { | ||
info: { | ||
title: 'dummy', | ||
version: '1.0', | ||
}, | ||
paths: {}, | ||
swagger: '2', | ||
}; | ||
parse(spec, options); | ||
expect(spy).toHaveBeenCalledWith(spec, options); | ||
|
||
const spec2: Parameters<typeof parse>[0] = { | ||
info: { | ||
title: 'dummy', | ||
version: '1.0', | ||
}, | ||
paths: {}, | ||
swagger: '2.0', | ||
}; | ||
parse(spec2, options); | ||
expect(spy).toHaveBeenCalledWith(spec2, options); | ||
}); | ||
|
||
it('uses v3 parser', () => { | ||
const spy = vi.spyOn(parseV3, 'parse'); | ||
|
||
const spec: Parameters<typeof parse>[0] = { | ||
info: { | ||
title: 'dummy', | ||
version: '1.0', | ||
}, | ||
openapi: '3', | ||
paths: {}, | ||
}; | ||
parse(spec, options); | ||
expect(spy).toHaveBeenCalledWith(spec, options); | ||
|
||
const spec2: Parameters<typeof parse>[0] = { | ||
info: { | ||
title: 'dummy', | ||
version: '1.0', | ||
}, | ||
openapi: '3.0', | ||
paths: {}, | ||
}; | ||
parse(spec2, options); | ||
expect(spy).toHaveBeenCalledWith(spec2, options); | ||
|
||
const spec3: Parameters<typeof parse>[0] = { | ||
info: { | ||
title: 'dummy', | ||
version: '1.0', | ||
}, | ||
openapi: '3.1.0', | ||
paths: {}, | ||
}; | ||
parse(spec3, options); | ||
expect(spy).toHaveBeenCalledWith(spec3, options); | ||
}); | ||
|
||
it('throws on unknown version', () => { | ||
// @ts-ignore | ||
expect(() => parse({ foo: 'bar' }, options)).toThrow( | ||
`Unsupported Open API specification: ${JSON.stringify({ foo: 'bar' }, null, 2)}` | ||
); | ||
}); | ||
}); |
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,4 @@ | ||
import type { OpenApi as OpenApiV2 } from '../../v2/interfaces/OpenApi'; | ||
import type { OpenApi as OpenApiV3 } from '../../v3/interfaces/OpenApi'; | ||
|
||
export type OpenApi = OpenApiV2 | OpenApiV3; |
36 changes: 34 additions & 2 deletions
36
src/openApi/v3/parser/getRef.spec.ts → ...pi/common/parser/__tests__/getRef.spec.ts
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,13 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { getServiceVersion } from '../service'; | ||
|
||
describe('getServiceVersion', () => { | ||
it.each([ | ||
{ input: '1.0', expected: '1.0' }, | ||
{ input: 'v1.2', expected: '1.2' }, | ||
{ input: 'V2.4', expected: '2.4' }, | ||
])('should get $expected when version is $input', ({ input, expected }) => { | ||
expect(getServiceVersion(input)).toEqual(expected); | ||
}); | ||
}); |
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,34 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { toSortedByRequired } from '../sort'; | ||
|
||
describe('sort', () => { | ||
it.each([ | ||
{ | ||
input: [ | ||
{ id: 'test', isRequired: false }, | ||
{ id: 'test2', isRequired: true }, | ||
{ id: 'test3', isRequired: true }, | ||
], | ||
expected: [ | ||
{ id: 'test2', isRequired: true }, | ||
{ id: 'test3', isRequired: true }, | ||
{ id: 'test', isRequired: false }, | ||
], | ||
}, | ||
{ | ||
input: [ | ||
{ id: 'test', isRequired: false }, | ||
{ id: 'test2', isRequired: false }, | ||
{ id: 'test3', isRequired: true, default: 'something' }, | ||
], | ||
expected: [ | ||
{ id: 'test', isRequired: false }, | ||
{ id: 'test2', isRequired: false }, | ||
{ id: 'test3', isRequired: true, default: 'something' }, | ||
], | ||
}, | ||
])('should sort $input by required to produce $expected', ({ input, expected }) => { | ||
expect(toSortedByRequired(input)).toEqual(expected); | ||
}); | ||
}); |
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,13 @@ | ||
/** | ||
* Sort list of values and ensure that required parameters are first so that we do not generate | ||
* invalid types. Optional parameters cannot be positioned after required ones. | ||
*/ | ||
export function toSortedByRequired<T extends { isRequired: boolean; default?: string }>(values: T[]): T[] { | ||
return values.sort((a, b) => { | ||
const aNeedsValue = a.isRequired && a.default === undefined; | ||
const bNeedsValue = b.isRequired && b.default === undefined; | ||
if (aNeedsValue && !bNeedsValue) return -1; | ||
if (bNeedsValue && !aNeedsValue) return 1; | ||
return 0; | ||
}); | ||
} |
Oops, something went wrong.