Skip to content

Commit e5eaddc

Browse files
feat(apidom-converter): add licenseIdentifierRefractorPlugin
This plugin removes the identifier fixed field of License Object. Refs #3697
1 parent 43751a9 commit e5eaddc

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import securityRequirementsEmptyRolesRefractorPlugin from './refractor-plugins/s
2323
import type { ConverterOptions } from '../../options';
2424
import createToolbox from './toolbox';
2525
import infoSummaryRefractorPlugin from './refractor-plugins/info-summary';
26+
import licenseIdentifierRefractorPlugin from './refractor-plugins/license-identifier';
2627

2728
// eslint-disable-next-line @typescript-eslint/naming-convention
2829
const openAPI3_0_3MediaTypes = [
@@ -65,6 +66,7 @@ class OpenAPI31ToOpenAPI30ConvertStrategy extends ConvertStrategy {
6566
securitySchemeTypeRefractorPlugin({ annotations }),
6667
securityRequirementsEmptyRolesRefractorPlugin({ annotations }),
6768
infoSummaryRefractorPlugin({ annotations }),
69+
licenseIdentifierRefractorPlugin({ annotations }),
6870
],
6971
{
7072
toolboxCreator: createToolbox,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { LicenseElement } from '@swagger-api/apidom-ns-openapi-3-1';
2+
import { AnnotationElement } from '@swagger-api/apidom-core';
3+
4+
type LicenseIdentifierPluginOptions = {
5+
annotations: AnnotationElement[];
6+
};
7+
8+
const licenseIdentifierRefractorPlugin =
9+
({ annotations }: LicenseIdentifierPluginOptions) =>
10+
() => {
11+
const annotation = new AnnotationElement(
12+
'The "identifier" field of License Object is not supported in OpenAPI 3.0.3. It has been removed from the converted document.',
13+
{ classes: ['warning'] },
14+
{ code: 'license-identifier' },
15+
);
16+
17+
return {
18+
visitor: {
19+
LicenseElement(element: LicenseElement) {
20+
if (!element.hasKey('identifier')) return undefined;
21+
22+
annotations.push(annotation);
23+
element.remove('identifier');
24+
25+
return undefined;
26+
},
27+
},
28+
};
29+
};
30+
31+
export default licenseIdentifierRefractorPlugin;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`converter strategies openapi-3-1-to-openapi-3-0-3 license-identifier should remove License.identifier field 1`] = `
4+
{
5+
"openapi": "3.0.3",
6+
"info": {
7+
"title": "foo",
8+
"version": "1.0.0",
9+
"license": {
10+
"name": "Apache 2.0"
11+
}
12+
}
13+
}
14+
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"openapi": "3.1.0",
3+
"info": {
4+
"title": "foo",
5+
"version": "1.0.0",
6+
"license": {
7+
"name": "Apache 2.0",
8+
"identifier": "Apache-2.0"
9+
}
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import path from 'node:path';
2+
import { expect, assert } from 'chai';
3+
import { mediaTypes as openAPI31MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-1';
4+
import { mediaTypes as openAPI30MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-0';
5+
import {
6+
AnnotationElement,
7+
ParseResultElement,
8+
toJSON,
9+
includesClasses,
10+
} from '@swagger-api/apidom-core';
11+
12+
import convert from '../../../../../src';
13+
14+
describe('converter', function () {
15+
context('strategies', function () {
16+
context('openapi-3-1-to-openapi-3-0-3', function () {
17+
context('license-identifier', function () {
18+
const fixturePath = path.join(__dirname, 'fixtures', 'license-identifier.json');
19+
let convertedParseResult: ParseResultElement;
20+
21+
beforeEach(async function () {
22+
convertedParseResult = await convert(fixturePath, {
23+
convert: {
24+
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),
25+
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'),
26+
},
27+
});
28+
});
29+
30+
specify('should remove License.identifier field', async function () {
31+
expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot();
32+
});
33+
34+
specify('should create WARNING annotation', async function () {
35+
const annotations = Array.from(convertedParseResult.annotations);
36+
const annotation = annotations.find((a: AnnotationElement) =>
37+
a.code?.equals('license-identifier'),
38+
);
39+
40+
assert.isDefined(annotation);
41+
assert.isTrue(includesClasses(['warning'], annotation));
42+
});
43+
});
44+
});
45+
});
46+
});

0 commit comments

Comments
 (0)