Skip to content

Commit 11bbea0

Browse files
sjorobekovCopilot
andauthored
feat: allow configuration of authPolicy for swagger page (#13)
* feat: allow configuration of authPolicy for swagger page * fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> * Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 3699808 commit 11bbea0

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ app.run(); // Open http://localhost:3030/api/docs
8282
| `enabled` | `true` | Convenience flag—skip calling `registerRoutes` if you want to hide docs. |
8383
| `path` | `'/api/docs'` | Mount path for Swagger UI; value is used as-is. |
8484
| `swaggerJsonPath` | `undefined` | Path relative to mount path where OpenAPI schema is served as JSON. When set, Swagger UI loads the schema from this endpoint instead of embedding it directly. |
85+
| `authPolicy` | `AuthPolicy.disabled` | Controls authentication for the Swagger UI page itself. |
8586

8687
Usage example:
8788

src/openapi-registry.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ export function createOpenApiRegistry(config: OpenApiRegistryConfig) {
403403
return {
404404
...routes,
405405
[`MOUNT ${mountPath}`]: {
406+
authPolicy: config.authPolicy ?? AuthPolicy.disabled,
406407
handler: ({router}: Parameters<AppMountHandler>[0]) => {
407408
const schema = getOpenApiSchema();
408409

src/tests/openapi-registry.test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import {createOpenApiRegistry} from '../openapi-registry';
22
import {apiKeyAuth, bearerAuth} from '../security-schemas';
3-
import {AppRoutes, AuthPolicy, RouteContract, withContract} from '@gravity-ui/expresskit';
3+
import {
4+
AppMountDescription,
5+
AppRoutes,
6+
AuthPolicy,
7+
RouteContract,
8+
withContract,
9+
} from '@gravity-ui/expresskit';
410
import {NodeKit} from '@gravity-ui/nodekit';
511
import {z} from 'zod';
612

@@ -669,6 +675,33 @@ describe('openapi-registry', () => {
669675

670676
const registeredRoutes = registerRoutes(routes, nodekit);
671677
expect(registeredRoutes).toHaveProperty('MOUNT /api/docs');
678+
const mountRoute = registeredRoutes['MOUNT /api/docs'] as AppMountDescription;
679+
expect(mountRoute).toBeDefined();
680+
expect(mountRoute.authPolicy).toBe(AuthPolicy.disabled);
681+
});
682+
683+
it('should apply configured authPolicy to MOUNT route', () => {
684+
const {registerRoutes} = createOpenApiRegistry({
685+
title: 'Test API',
686+
authPolicy: AuthPolicy.required,
687+
});
688+
689+
const routes = {
690+
'GET /test': {
691+
handler: withContract({
692+
request: {},
693+
response: {content: {200: z.object({})}},
694+
})(async (_req, res) => {
695+
res.sendTyped(200, {});
696+
}),
697+
},
698+
};
699+
700+
const registeredRoutes = registerRoutes(routes, nodekit);
701+
const mountRoute = registeredRoutes['MOUNT /api/docs'] as AppMountDescription;
702+
703+
expect(mountRoute).toBeDefined();
704+
expect(mountRoute.authPolicy).toBe(AuthPolicy.required);
672705
});
673706

674707
it('should handle routes with tags and description', () => {

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type {SwaggerUiOptions} from 'swagger-ui-express';
2-
import type {AppRouteDescription} from '@gravity-ui/expresskit';
2+
import type {AppRouteDescription, AuthPolicy} from '@gravity-ui/expresskit';
33

44
// OpenAPI Security Scheme Object types
55
export interface SecuritySchemeObject {
@@ -64,6 +64,7 @@ export interface OpenApiRegistryConfig {
6464
}[];
6565
swaggerUi?: SwaggerUiOptions;
6666
swaggerJsonPath?: string;
67+
authPolicy?: AuthPolicy;
6768
transformOperation?: (
6869
operation: OpenApiOperation,
6970
context: {

0 commit comments

Comments
 (0)