diff --git a/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/security-scheme-type.ts b/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/security-scheme-type.ts index f77070057..c5c344321 100644 --- a/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/security-scheme-type.ts +++ b/packages/apidom-converter/src/strategies/openapi-3-1-to-openapi-3-0-3/refractor-plugins/security-scheme-type.ts @@ -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[]; @@ -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); @@ -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);