Skip to content

Commit

Permalink
feat(apidom-converter): add licenseIdentifierRefractorPlugin (#3854)
Browse files Browse the repository at this point in the history
This plugin removes the identifier fixed field of License Object.

Refs #3697
  • Loading branch information
kowalczyk-krzysztof committed Feb 23, 2024
1 parent 43751a9 commit b28cafb
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
Expand Up @@ -23,6 +23,7 @@ import securityRequirementsEmptyRolesRefractorPlugin from './refractor-plugins/s
import type { ConverterOptions } from '../../options';
import createToolbox from './toolbox';
import infoSummaryRefractorPlugin from './refractor-plugins/info-summary';
import licenseIdentifierRefractorPlugin from './refractor-plugins/license-identifier';

// eslint-disable-next-line @typescript-eslint/naming-convention
const openAPI3_0_3MediaTypes = [
Expand Down Expand Up @@ -65,6 +66,7 @@ class OpenAPI31ToOpenAPI30ConvertStrategy extends ConvertStrategy {
securitySchemeTypeRefractorPlugin({ annotations }),
securityRequirementsEmptyRolesRefractorPlugin({ annotations }),
infoSummaryRefractorPlugin({ annotations }),
licenseIdentifierRefractorPlugin({ annotations }),
],
{
toolboxCreator: createToolbox,
Expand Down
@@ -0,0 +1,31 @@
import { LicenseElement } from '@swagger-api/apidom-ns-openapi-3-1';
import { AnnotationElement } from '@swagger-api/apidom-core';

type LicenseIdentifierPluginOptions = {
annotations: AnnotationElement[];
};

const licenseIdentifierRefractorPlugin =
({ annotations }: LicenseIdentifierPluginOptions) =>
() => {
const annotation = new AnnotationElement(
'The "identifier" field of License Object is not supported in OpenAPI 3.0.3. It has been removed from the converted document.',
{ classes: ['warning'] },
{ code: 'license-identifier' },
);

return {
visitor: {
LicenseElement(element: LicenseElement) {
if (!element.hasKey('identifier')) return undefined;

annotations.push(annotation);
element.remove('identifier');

return undefined;
},
},
};
};

export default licenseIdentifierRefractorPlugin;
@@ -0,0 +1,14 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`converter strategies openapi-3-1-to-openapi-3-0-3 license-identifier should remove License.identifier field 1`] = `
{
"openapi": "3.0.3",
"info": {
"title": "foo",
"version": "1.0.0",
"license": {
"name": "Apache 2.0"
}
}
}
`;
@@ -0,0 +1,11 @@
{
"openapi": "3.1.0",
"info": {
"title": "foo",
"version": "1.0.0",
"license": {
"name": "Apache 2.0",
"identifier": "Apache-2.0"
}
}
}
@@ -0,0 +1,46 @@
import path from 'node:path';
import { expect, assert } from 'chai';
import { mediaTypes as openAPI31MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-1';
import { mediaTypes as openAPI30MediaTypes } from '@swagger-api/apidom-parser-adapter-openapi-json-3-0';
import {
AnnotationElement,
ParseResultElement,
toJSON,
includesClasses,
} from '@swagger-api/apidom-core';

import convert from '../../../../../src';

describe('converter', function () {
context('strategies', function () {
context('openapi-3-1-to-openapi-3-0-3', function () {
context('license-identifier', function () {
const fixturePath = path.join(__dirname, 'fixtures', 'license-identifier.json');
let convertedParseResult: ParseResultElement;

beforeEach(async function () {
convertedParseResult = await convert(fixturePath, {
convert: {
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'),
},
});
});

specify('should remove License.identifier field', async function () {
expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot();
});

specify('should create WARNING annotation', async function () {
const annotations = Array.from(convertedParseResult.annotations);
const annotation = annotations.find((a: AnnotationElement) =>
a.code?.equals('license-identifier'),
);

assert.isDefined(annotation);
assert.isTrue(includesClasses(['warning'], annotation));
});
});
});
});
});

0 comments on commit b28cafb

Please sign in to comment.