-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apidom-converter): add example plugin
- Loading branch information
1 parent
946f72d
commit 9c7ace8
Showing
8 changed files
with
101 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
import { OpenApi3_0Element } from '@swagger-api/apidom-ns-openapi-3-0'; | ||
import { SwaggerElement } from '@swagger-api/apidom-ns-openapi-2'; | ||
|
||
const getOpenApiRefractor = (version: string) => { | ||
switch (version) { | ||
case '3.1.x': | ||
return OpenApi3_1Element; | ||
case '3.0.x': | ||
return OpenApi3_0Element; | ||
case '2.0.x': | ||
return SwaggerElement; | ||
default: | ||
throw new Error(`Unsupported OpenAPI version: ${version}`); | ||
} | ||
}; | ||
|
||
export default getOpenApiRefractor; |
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 |
---|---|---|
@@ -1,3 +1,15 @@ | ||
const foo = Symbol('foo'); | ||
import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2'; | ||
|
||
export default foo; | ||
import getOpenAPIRefractor from './get-refractor'; | ||
import getPluginsBySpec from './plugins/get-plugins-by-spec'; | ||
|
||
const convert = async (yaml: string, from: string) => { | ||
const apiDOM = await parse(yaml); | ||
const refractor = getOpenAPIRefractor(from); | ||
const openApiElement = refractor.refract(apiDOM.result, { | ||
plugins: [...getPluginsBySpec(from)], | ||
}) as unknown as typeof refractor; | ||
return openApiElement; | ||
}; | ||
|
||
export default convert; |
16 changes: 16 additions & 0 deletions
16
packages/apidom-converter/src/plugins/get-plugins-by-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import specDowngrade from './openapi3_1/spec-downgrade'; | ||
|
||
const getPluginsBySpec = (spec: string) => { | ||
switch (spec) { | ||
case '3.1.x': | ||
return [specDowngrade()]; | ||
case '3.0.x': | ||
return []; | ||
case '2.0.x': | ||
return []; | ||
default: | ||
return []; | ||
} | ||
}; | ||
|
||
export default getPluginsBySpec; |
13 changes: 13 additions & 0 deletions
13
packages/apidom-converter/src/plugins/openapi3_1/spec-downgrade.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1'; | ||
|
||
const plugin = () => () => { | ||
return { | ||
visitor: { | ||
OpenApi3_1Element(openApi3_1Element: OpenApi3_1Element) { | ||
openApi3_1Element.set('openapi', '3.0.0'); | ||
}, | ||
}, | ||
}; | ||
}; | ||
|
||
export default plugin; |
7 changes: 7 additions & 0 deletions
7
packages/apidom-converter/test/plugins/spec-downgrade/__snapshots__/index.ts.snap
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,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`refractor plugins info-downgrade should downgrade version to 3.0.0 1`] = ` | ||
Object { | ||
openapi: 3.0.0, | ||
} | ||
`; |
20 changes: 20 additions & 0 deletions
20
packages/apidom-converter/test/plugins/spec-downgrade/index.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import dedent from 'dedent'; | ||
import { toValue } from '@swagger-api/apidom-core'; | ||
import { expect } from 'chai'; | ||
|
||
import convert from '../../../src'; | ||
|
||
describe('converter', function () { | ||
context('plugins', function () { | ||
context('spec-downgrade', function () { | ||
specify('should downgrade OpenAPI specificaiton to 3.0.0', async function () { | ||
const yamlDefinition = dedent` | ||
openapi: 3.1.0 | ||
`; | ||
const openApiElement = await convert(yamlDefinition, '3.1.x'); | ||
|
||
expect(toValue(openApiElement)).toMatchSnapshot(); | ||
}); | ||
}); | ||
}); | ||
}); |