File tree 8 files changed +101
-5
lines changed
packages/apidom-converter
test/plugins/spec-downgrade 8 files changed +101
-5
lines changed Original file line number Diff line number Diff line change 38
38
"author" : " Vladimír Gorej" ,
39
39
"license" : " Apache-2.0" ,
40
40
"dependencies" : {
41
- "@babel/runtime-corejs3" : " ^7.20.7"
41
+ "@babel/runtime-corejs3" : " ^7.20.7" ,
42
+ "@swagger-api/apidom-core" : " ^0.93.0" ,
43
+ "@swagger-api/apidom-parser-adapter-yaml-1-2" : " ^0.93.0" ,
44
+ "@swagger-api/apidom-ns-openapi-3-1" : " ^0.93.0" ,
45
+ "@swagger-api/apidom-ns-openapi-3-0" : " ^0.93.0" ,
46
+ "@swagger-api/apidom-ns-openapi-2" : " ^0.93.0"
42
47
},
43
48
"files" : [
44
49
" cjs/" ,
51
56
" README.md" ,
52
57
" CHANGELOG.md"
53
58
]
54
- }
59
+ }
Original file line number Diff line number Diff line change
1
+ import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1' ;
2
+ import { OpenApi3_0Element } from '@swagger-api/apidom-ns-openapi-3-0' ;
3
+ import { SwaggerElement } from '@swagger-api/apidom-ns-openapi-2' ;
4
+
5
+ const getOpenApiRefractor = ( version : string ) => {
6
+ switch ( version ) {
7
+ case '3.1.x' :
8
+ return OpenApi3_1Element ;
9
+ case '3.0.x' :
10
+ return OpenApi3_0Element ;
11
+ case '2.0.x' :
12
+ return SwaggerElement ;
13
+ default :
14
+ throw new Error ( `Unsupported OpenAPI version: ${ version } ` ) ;
15
+ }
16
+ } ;
17
+
18
+ export default getOpenApiRefractor ;
Original file line number Diff line number Diff line change 1
- const foo = Symbol ( 'foo' ) ;
1
+ import { parse } from '@swagger-api/apidom-parser-adapter-yaml-1-2' ;
2
2
3
- export default foo ;
3
+ import getOpenAPIRefractor from './get-refractor' ;
4
+ import getPluginsBySpec from './plugins/get-plugins-by-spec' ;
5
+
6
+ const convert = async ( yaml : string , from : string ) => {
7
+ const apiDOM = await parse ( yaml ) ;
8
+ const refractor = getOpenAPIRefractor ( from ) ;
9
+ const openApiElement = refractor . refract ( apiDOM . result , {
10
+ plugins : [ ...getPluginsBySpec ( from ) ] ,
11
+ } ) as unknown as typeof refractor ;
12
+ return openApiElement ;
13
+ } ;
14
+
15
+ export default convert ;
Original file line number Diff line number Diff line change
1
+ import specDowngrade from './openapi3_1/spec-downgrade' ;
2
+
3
+ const getPluginsBySpec = ( spec : string ) => {
4
+ switch ( spec ) {
5
+ case '3.1.x' :
6
+ return [ specDowngrade ( ) ] ;
7
+ case '3.0.x' :
8
+ return [ ] ;
9
+ case '2.0.x' :
10
+ return [ ] ;
11
+ default :
12
+ return [ ] ;
13
+ }
14
+ } ;
15
+
16
+ export default getPluginsBySpec ;
Original file line number Diff line number Diff line change
1
+ import { OpenApi3_1Element } from '@swagger-api/apidom-ns-openapi-3-1' ;
2
+
3
+ const plugin = ( ) => ( ) => {
4
+ return {
5
+ visitor : {
6
+ OpenApi3_1Element ( openApi3_1Element : OpenApi3_1Element ) {
7
+ openApi3_1Element . set ( 'openapi' , '3.0.0' ) ;
8
+ } ,
9
+ } ,
10
+ } ;
11
+ } ;
12
+
13
+ export default plugin ;
Original file line number Diff line number Diff line change
1
+ // Jest Snapshot v1, https://goo.gl/fbAQLP
2
+
3
+ exports [` refractor plugins info-downgrade should downgrade version to 3.0.0 1` ] = `
4
+ Object {
5
+ openapi : 3.0 .0 ,
6
+ }
7
+ ` ;
Original file line number Diff line number Diff line change
1
+ import dedent from 'dedent' ;
2
+ import { toValue } from '@swagger-api/apidom-core' ;
3
+ import { expect } from 'chai' ;
4
+
5
+ import convert from '../../../src' ;
6
+
7
+ describe ( 'converter' , function ( ) {
8
+ context ( 'plugins' , function ( ) {
9
+ context ( 'spec-downgrade' , function ( ) {
10
+ specify ( 'should downgrade OpenAPI specificaiton to 3.0.0' , async function ( ) {
11
+ const yamlDefinition = dedent `
12
+ openapi: 3.1.0
13
+ ` ;
14
+ const openApiElement = await convert ( yamlDefinition , '3.1.x' ) ;
15
+
16
+ expect ( toValue ( openApiElement ) ) . toMatchSnapshot ( ) ;
17
+ } ) ;
18
+ } ) ;
19
+ } ) ;
20
+ } ) ;
You can’t perform that action at this time.
0 commit comments