Skip to content

Commit

Permalink
feat(apidom-converter): add securitySchemeTypeRefractorPlugin
Browse files Browse the repository at this point in the history
refs: SWG-9963
  • Loading branch information
kowalczyk-krzysztof committed Feb 6, 2024
1 parent 6e1fe1c commit e3defde
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
import ConvertStrategy, { IFile } from '../ConvertStrategy';
import openAPIVersionRefractorPlugin from './refractor-plugins/openapi-version';
import webhooksRefractorPlugin from './refractor-plugins/webhooks';
import securitySchemeTypeRefractorPlugin from './refractor-plugins/security-scheme-type';
import type { ConverterOptions } from '../../options';

// eslint-disable-next-line @typescript-eslint/naming-convention
Expand Down Expand Up @@ -57,7 +58,11 @@ class OpenAPI31ToOpenAPI30ConvertStrategy extends ConvertStrategy {
const annotations: AnnotationElement[] = [];
const converted = dispatchRefractorPlugins(
parseResultElement,
[openAPIVersionRefractorPlugin(), webhooksRefractorPlugin({ annotations })],
[
openAPIVersionRefractorPlugin(),
webhooksRefractorPlugin({ annotations }),
securitySchemeTypeRefractorPlugin({ annotations }),
],
{
toolboxCreator: createToolbox,
visitorOptions: { keyMap, nodeTypeGetter: getNodeType },
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { SecuritySchemeElement } from '@swagger-api/apidom-ns-openapi-3-1';
import { AnnotationElement } from '@swagger-api/apidom-core';

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

const securitySchemeTypeRefractorPlugin =
({ annotations }: SecuritySchemeTypePluginOptions) =>
() => ({
visitor: {
SecuritySchemeElement(element: SecuritySchemeElement) {
if (element.get('type')?.toValue() === 'mutualTLS') {
const annotation = new AnnotationElement(
'"mutualTLS" type is not supported in OpenAPI 3.0.3.',
{ classes: ['warning'] },
{ code: 'mutualTLS' },
);

annotations.push(annotation);
}

return element;
},
},
});

export default securitySchemeTypeRefractorPlugin;
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`converter strategies openapi-3-1-to-openapi-3-0-3 security-scheme-type converted definition should remain the same 1`] = `
{
"openapi": "3.0.3",
"components": {
"securitySchemes": {
"mutualTLS": {
"type": "mutualTLS",
"name": "mutualTLS",
"in": "header"
},
"apiKey": {
"type": "apiKey",
"name": "api_key",
"in": "header"
}
}
}
}
`;
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"openapi": "3.1.0",
"components": {
"securitySchemes": {
"mutualTLS": {
"type": "mutualTLS",
"name": "mutualTLS",
"in": "header"
},
"apiKey": {
"type": "apiKey",
"name": "api_key",
"in": "header"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import path from 'node:path';
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, includesClasses, toJSON } from '@swagger-api/apidom-core';
import { assert, expect } from 'chai';

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

describe('converter', function () {
context('strategies', function () {
context('openapi-3-1-to-openapi-3-0-3', function () {
context('security-scheme-type', function () {
specify('converted definition should remain the same', async function () {
const fixturePath = path.join(__dirname, 'fixtures', 'security-scheme-type.json');
const convertedParseResult = await convert(fixturePath, {
convert: {
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'),
},
});

expect(toJSON(convertedParseResult.api!, undefined, 2)).toMatchSnapshot();
});

specify('should create WARNING annotation', async function () {
const fixturePath = path.join(__dirname, 'fixtures', 'security-scheme-type.json');
const convertedParseResult = await convert(fixturePath, {
convert: {
sourceMediaType: openAPI31MediaTypes.findBy('3.1.0', 'json'),
targetMediaType: openAPI30MediaTypes.findBy('3.0.3', 'json'),
},
});
const annotations = Array.from(convertedParseResult.annotations);
const annotation = annotations.find((a: AnnotationElement) =>
a.code?.equals('mutualTLS'),
);

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

0 comments on commit e3defde

Please sign in to comment.