-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(apidom-converter): add licenseIdentifierRefractorPlugin
This plugin removes the identifier fixed field of License Object. Refs #3697
- Loading branch information
1 parent
43751a9
commit e5eaddc
Showing
5 changed files
with
104 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...erter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
14 changes: 14 additions & 0 deletions
14
...api-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/__snapshots__/index.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} | ||
} | ||
`; |
11 changes: 11 additions & 0 deletions
11
...-1-to-openapi-3-0-3/refractor-plugins/license-identifier/fixtures/license-identifier.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"openapi": "3.1.0", | ||
"info": { | ||
"title": "foo", | ||
"version": "1.0.0", | ||
"license": { | ||
"name": "Apache 2.0", | ||
"identifier": "Apache-2.0" | ||
} | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...est/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/license-identifier/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |