Skip to content

Handle optional security headers #3128

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/stupid-hats-reflect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@gitbook/react-openapi': patch
---

Handle optional security headers
31 changes: 22 additions & 9 deletions packages/react-openapi/src/OpenAPISecurities.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import type { OpenAPIV3_1 } from '@gitbook/openapi-parser';
import { InteractiveSection } from './InteractiveSection';
import { Markdown } from './Markdown';
import { OpenAPISchemaName } from './OpenAPISchemaName';
import type { OpenAPIClientContext, OpenAPIOperationData } from './types';
import type {
OpenAPIClientContext,
OpenAPIOperationData,
OpenAPISecurityWithRequired,
} from './types';
import { resolveDescription } from './utils';

/**
Expand Down Expand Up @@ -50,26 +53,36 @@ export function OpenAPISecurities(props: {
);
}

function getLabelForType(security: OpenAPIV3_1.SecuritySchemeObject) {
function getLabelForType(security: OpenAPISecurityWithRequired) {
switch (security.type) {
case 'apiKey':
return (
<OpenAPISchemaName
propertyName={security.name ?? 'apiKey'}
type="string"
required
required={security.required}
/>
);
case 'http':
if (security.scheme === 'basic') {
return <OpenAPISchemaName propertyName="Authorization" type="string" required />;
return (
<OpenAPISchemaName
propertyName="Authorization"
type="string"
required={security.required}
/>
);
}

if (security.scheme === 'bearer') {
const description = resolveDescription(security);
return (
<>
<OpenAPISchemaName propertyName="Authorization" type="string" required />
<OpenAPISchemaName
propertyName="Authorization"
type="string"
required={security.required}
/>
{/** Show a default description if none is provided */}
{!description ? (
<Markdown
Expand All @@ -81,11 +94,11 @@ function getLabelForType(security: OpenAPIV3_1.SecuritySchemeObject) {
);
}

return <OpenAPISchemaName propertyName="HTTP" required />;
return <OpenAPISchemaName propertyName="HTTP" required={security.required} />;
case 'oauth2':
return <OpenAPISchemaName propertyName="OAuth2" required />;
return <OpenAPISchemaName propertyName="OAuth2" required={security.required} />;
case 'openIdConnect':
return <OpenAPISchemaName propertyName="OpenID Connect" required />;
return <OpenAPISchemaName propertyName="OpenID Connect" required={security.required} />;
default:
// @ts-ignore
return security.type;
Expand Down
17 changes: 14 additions & 3 deletions packages/react-openapi/src/resolveOpenAPIOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,27 @@ export async function resolveOpenAPIOperation(
}

const servers = 'servers' in schema ? (schema.servers ?? []) : [];
const security = flattenSecurities(operation.security ?? schema.security ?? []);
const security: OpenAPIV3_1.SecurityRequirementObject[] =
operation.security ?? schema.security ?? [];

// If security includes an empty object, it means that the security is optional
const isOptionalSecurity = security.some((entry) => Object.keys(entry).length === 0);
const flatSecurities = flattenSecurities(security);

// Resolve securities
const securities: OpenAPIOperationData['securities'] = [];
for (const entry of security) {
for (const entry of flatSecurities) {
const securityKey = Object.keys(entry)[0];
if (securityKey) {
const securityScheme = schema.components?.securitySchemes?.[securityKey];
if (securityScheme && !checkIsReference(securityScheme)) {
securities.push([securityKey, securityScheme]);
securities.push([
securityKey,
{
...securityScheme,
required: !isOptionalSecurity,
},
]);
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion packages/react-openapi/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export interface OpenAPIContext extends OpenAPIClientContext {
specUrl: string;
}

export type OpenAPISecurityWithRequired = OpenAPIV3.SecuritySchemeObject & { required?: boolean };

export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
path: string;
method: string;
Expand All @@ -68,5 +70,5 @@ export interface OpenAPIOperationData extends OpenAPICustomSpecProperties {
operation: OpenAPIV3.OperationObject<OpenAPICustomOperationProperties>;

/** Securities that should be used for this operation */
securities: [string, OpenAPIV3.SecuritySchemeObject][];
securities: [string, OpenAPISecurityWithRequired][];
}