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 53fc446
Showing 1 changed file with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ import {
OpenApi3_1Element,
SecurityRequirementElement,
SecuritySchemeElement,
isSecuritySchemeElement,
isComponentsElement,
} 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';

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

element.components.securitySchemes.forEach((value) => {
if (isSecuritySchemeElement(value) && toValue(value.type) === 'mutualTLS') {
removedSecuritySchemes.push(value);
}
});

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,11 +55,15 @@ 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 53fc446

Please sign in to comment.