Skip to content

Commit

Permalink
feat(apidom-converter): add missing checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Feb 12, 2024
1 parent ba2b3a0 commit 3aa4964
Showing 1 changed file with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,20 @@ import {
SecurityRequirementElement,
SecuritySchemeElement,
} from '@swagger-api/apidom-ns-openapi-3-1';
import { AnnotationElement, toValue, cloneShallow } from '@swagger-api/apidom-core';
import {
AnnotationElement,
toValue,
cloneShallow,
isObjectElement,
} from '@swagger-api/apidom-core';
import { isComponentsElement } from '@swagger-api/apidom-ns-openapi-3-0';

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

const isSecuritySchemeElement = (element: SecuritySchemeElement) => !!element; // TODO: Add import from @swagger-api/apidom-ns-openapi-3-1 when new release is released

const securitySchemeTypeRefractorPlugin =
({ annotations }: SecuritySchemeTypePluginOptions) =>
() => {
Expand All @@ -23,16 +31,25 @@ const securitySchemeTypeRefractorPlugin =
return {
visitor: {
OpenApi3_1Element(element: OpenApi3_1Element) {
element?.components?.securitySchemes?.forEach((el) => {
const securityScheme = el as SecuritySchemeElement;
if (toValue(securityScheme.type) === 'mutualTLS') {
if (!isComponentsElement(element.components)) return undefined;
if (!isObjectElement(element.components?.securitySchemes)) return undefined;

element.components.securitySchemes.forEach((securityScheme) => {
if (
// @ts-ignore TODO: See isSecuritySchemeElement comment
isSecuritySchemeElement(securityScheme) &&
// @ts-ignore TODO: See isSecuritySchemeElement comment
toValue(securityScheme.type) === 'mutualTLS'
) {
// @ts-ignore TODO: See isSecuritySchemeElement comment
removedSecuritySchemes.push(securityScheme);
}
});
return undefined;
},
ComponentsElement(element: ComponentsElement) {
const { securitySchemes } = element;
if (!securitySchemes) return undefined;
if (!securitySchemes || !removedSecuritySchemes.length) return undefined;

const clonedSecuritySchemes = cloneShallow(securitySchemes);

Expand All @@ -44,10 +61,13 @@ const securitySchemeTypeRefractorPlugin =
return clonedSecuritySchemes;
},
SecurityRequirementElement(element: SecurityRequirementElement) {
if (!removedSecuritySchemes.length) return undefined;

const elementName = Object.keys(toValue(element))[0];
const matchingSecurityScheme = removedSecuritySchemes.find(
(securityScheme) => toValue(securityScheme.name) === elementName,
);

if (!matchingSecurityScheme) return undefined;
const clonedElement = cloneShallow(element);
clonedElement.remove(elementName);
Expand Down

0 comments on commit 3aa4964

Please sign in to comment.