Skip to content

Commit

Permalink
feat(apidom-converter): mutable plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
kowalczyk-krzysztof committed Feb 12, 2024
1 parent d665498 commit c97b9ac
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ import {
SecuritySchemeElement,
isSecuritySchemeElement,
isComponentsElement,
isSecurityRequirementElement,
} from '@swagger-api/apidom-ns-openapi-3-1';
import {
AnnotationElement,
toValue,
cloneShallow,
isObjectElement,
} from '@swagger-api/apidom-core';
import { AnnotationElement, toValue, isObjectElement } from '@swagger-api/apidom-core';

type SecuritySchemeTypePluginOptions = {
annotations: AnnotationElement[];
Expand Down Expand Up @@ -42,33 +38,42 @@ const securitySchemeTypeRefractorPlugin =
return undefined;
},
ComponentsElement(element: ComponentsElement) {
const { securitySchemes } = element;
if (!securitySchemes || !removedSecuritySchemes.length) return undefined;

const clonedSecuritySchemes = cloneShallow(securitySchemes);
if (!isObjectElement(element.securitySchemes)) return undefined;

removedSecuritySchemes.forEach((securityScheme) => {
clonedSecuritySchemes.remove(toValue(securityScheme.name));
annotations.push(annotation);
element.securitySchemes.forEach((value, key) => {
if (isSecuritySchemeElement(value) && toValue(value.type) === 'mutualTLS') {
if (!removedSecuritySchemes.includes(value)) removedSecuritySchemes.push(value);
(element.securitySchemes as SecuritySchemeElement).remove(toValue(key));
annotations.push(annotation);
}
});

return clonedSecuritySchemes;
return undefined;
},
SecurityRequirementElement(element: SecurityRequirementElement) {
if (!removedSecuritySchemes.length) return undefined;
if (!removedSecuritySchemes.length || !isSecurityRequirementElement(element))
return undefined;

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

if (!matchingSecurityScheme) return undefined;
element.forEach((_, key) => {
const removedSecurityScheme = removedSecuritySchemes.find(
(securityScheme) => toValue(securityScheme.name) === toValue(key),
);

const clonedElement = cloneShallow(element);
clonedElement.remove(elementName);
if (isSecuritySchemeElement(removedSecurityScheme)) {
keysToRemove.push(toValue(key));
}
});

if (!keysToRemove.length) return undefined;

keysToRemove.forEach((key) => {
element.remove(key);
annotations.push(annotation);
});

annotations.push(annotation);
return clonedElement;
return undefined;
},
},
post() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ exports[`converter strategies openapi-3-1-to-openapi-3-0-3 security-scheme-type
}
},
"components": {
"apiKey-scheme": {
"type": "apiKey",
"name": "apiKey-scheme",
"in": "header"
"securitySchemes": {
"apiKey-scheme": {
"type": "apiKey",
"name": "apiKey-scheme",
"in": "header"
}
}
}
}
Expand Down

0 comments on commit c97b9ac

Please sign in to comment.