Skip to content

feat(apidom-converter): add example plugin #3743

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[`converter plugins spec-downgrade should downgrade OpenAPI specificaiton 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();
});
});
});
});