From e5eaddc56e164cff060cfc43b1e6633a10711e39 Mon Sep 17 00:00:00 2001 From: Krzysztof Kowalczyk Date: Fri, 23 Feb 2024 10:31:39 +0100 Subject: [PATCH] feat(apidom-converter): add licenseIdentifierRefractorPlugin This plugin removes the identifier fixed field of License Object. Refs #3697 --- .../openapi-3-1-to-openapi-3-0-3/index.ts | 2 + .../refractor-plugins/license-identifier.ts | 31 +++++++++++++ .../__snapshots__/index.ts.snap | 14 ++++++ .../fixtures/license-identifier.json | 11 +++++ .../license-identifier/index.ts | 46 +++++++++++++++++++ 5 files changed, 104 insertions(+) create mode 100644 packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier.ts create mode 100644 packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/__snapshots__/index.ts.snap create mode 100644 packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/fixtures/license-identifier.json create mode 100644 packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/index.ts diff --git a/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/index.ts b/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/index.ts index a78cdcbb7..a4c76b15a 100644 --- a/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/index.ts +++ b/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/index.ts @@ -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 = [ @@ -65,6 +66,7 @@ class OpenAPI31ToOpenAPI30ConvertStrategy extends ConvertStrategy { securitySchemeTypeRefractorPlugin({ annotations }), securityRequirementsEmptyRolesRefractorPlugin({ annotations }), infoSummaryRefractorPlugin({ annotations }), + licenseIdentifierRefractorPlugin({ annotations }), ], { toolboxCreator: createToolbox, diff --git a/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier.ts b/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier.ts new file mode 100644 index 000000000..a451c35e7 --- /dev/null +++ b/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier.ts @@ -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; diff --git a/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/__snapshots__/index.ts.snap b/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/__snapshots__/index.ts.snap new file mode 100644 index 000000000..4564ee050 --- /dev/null +++ b/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/__snapshots__/index.ts.snap @@ -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" + } + } +} +`; diff --git a/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/fixtures/license-identifier.json b/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/fixtures/license-identifier.json new file mode 100644 index 000000000..129da658c --- /dev/null +++ b/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/fixtures/license-identifier.json @@ -0,0 +1,11 @@ +{ + "openapi": "3.1.0", + "info": { + "title": "foo", + "version": "1.0.0", + "license": { + "name": "Apache 2.0", + "identifier": "Apache-2.0" + } + } +} \ No newline at end of file diff --git a/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/index.ts b/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/index.ts new file mode 100644 index 000000000..e5c88055a --- /dev/null +++ b/packages/apidom-converter/test/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/index.ts @@ -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)); + }); + }); + }); + }); +});