Skip to content

Commit

Permalink
feat(apidom-converter): add example plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Jan 27, 2024
1 parent 946f72d commit 9c7ace8
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 5 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions packages/apidom-converter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@
"author": "Vladimír Gorej",
"license": "Apache-2.0",
"dependencies": {
"@babel/runtime-corejs3": "^7.20.7"
"@babel/runtime-corejs3": "^7.20.7",
"@swagger-api/apidom-core": "^0.93.0",
"@swagger-api/apidom-parser-adapter-yaml-1-2": "^0.93.0",
"@swagger-api/apidom-ns-openapi-3-1": "^0.93.0",
"@swagger-api/apidom-ns-openapi-3-0": "^0.93.0",
"@swagger-api/apidom-ns-openapi-2": "^0.93.0"
},
"files": [
"cjs/",
Expand All @@ -51,4 +56,4 @@
"README.md",
"CHANGELOG.md"
]
}
}
18 changes: 18 additions & 0 deletions packages/apidom-converter/src/get-refractor.ts
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;
16 changes: 14 additions & 2 deletions packages/apidom-converter/src/index.ts
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 packages/apidom-converter/src/plugins/get-plugins-by-spec.ts
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 packages/apidom-converter/src/plugins/openapi3_1/spec-downgrade.ts
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;
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 packages/apidom-converter/test/plugins/spec-downgrade/index.ts
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();
});
});
});
});

0 comments on commit 9c7ace8

Please sign in to comment.