Skip to content

Commit dda0cc6

Browse files
authored
Flatten OpenAPI security object (#2818)
1 parent 727bde2 commit dda0cc6

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

.changeset/mighty-cheetahs-jog.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@gitbook/react-openapi': patch
3+
---
4+
5+
Flatten OpenAPI security object

packages/react-openapi/src/fetchOpenAPIOperation.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export async function fetchOpenAPIOperation(
6464
}
6565

6666
const servers = 'servers' in schema ? (schema.servers ?? []) : [];
67-
const security = operation.security ?? schema.security ?? [];
67+
const security = flattenSecurities(operation.security ?? schema.security ?? []);
6868

6969
// Resolve securities
7070
const securities: OpenAPIOperationData['securities'] = [];
@@ -179,3 +179,19 @@ function getOperationByPathAndMethod(
179179
}
180180
return pathObject[normalizedMethod];
181181
}
182+
183+
/**
184+
* Flatten security objects in case they are nested.
185+
* @example [{bearerAuth:[], basicAuth:[]}] => [{ bearerAuth: [] }, { basicAuth: [] }]
186+
*/
187+
function flattenSecurities(security: OpenAPIV3.SecurityRequirementObject[]) {
188+
if (!Array.isArray(security) || security.length === 0) {
189+
return [];
190+
}
191+
192+
return security.flatMap((securityObject) => {
193+
return Object.entries(securityObject).map(([authType, config]) => ({
194+
[authType]: config,
195+
}));
196+
});
197+
}

0 commit comments

Comments
 (0)