From 255e4db12c4cd727aec5e884fcbebdcc7d0f986e Mon Sep 17 00:00:00 2001 From: Uladzislau Rastargueu Date: Sat, 30 Nov 2024 00:35:54 +0100 Subject: [PATCH 1/2] feat(OpenApiNestFactory): new optional attribute `customOptions` was added to `webServerOptions`. Use it to pass custom options to SwaggerModule `setup` method (applicable only when web server is enabled) --- src/openapi.service.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/openapi.service.ts b/src/openapi.service.ts index ba6540d..8110311 100644 --- a/src/openapi.service.ts +++ b/src/openapi.service.ts @@ -3,6 +3,7 @@ import { SwaggerModule, OpenAPIObject, SwaggerDocumentOptions, + SwaggerCustomOptions, } from '@nestjs/swagger'; import { INestApplication, Injectable } from '@nestjs/common'; import { @@ -17,6 +18,7 @@ import { export interface OpenApiWebServerOptions { enabled: boolean; path: string; + customOptions?: SwaggerCustomOptions; } export interface OpenApiOptions { @@ -69,7 +71,7 @@ export class OpenApiService { document: OpenAPIObject, options: OpenApiWebServerOptions, ) { - SwaggerModule.setup(options.path ?? 'apidocs', app, document); + SwaggerModule.setup(options.path ?? 'apidocs', app, document, options.customOptions); } private async generateOpenApiFile( From 8d147d442a81878812a93e7b8a052f32098386e1 Mon Sep 17 00:00:00 2001 From: Uladzislau Rastargueu Date: Sat, 30 Nov 2024 01:37:13 +0100 Subject: [PATCH 2/2] chore(): add test for `customOptions` attribute in `webServerOptions` --- src/openapi-nest.factory.spec.ts | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/src/openapi-nest.factory.spec.ts b/src/openapi-nest.factory.spec.ts index 0e7379e..ca3c2d8 100644 --- a/src/openapi-nest.factory.spec.ts +++ b/src/openapi-nest.factory.spec.ts @@ -2,8 +2,14 @@ import { NestFactory } from '@nestjs/core'; import { OpenApiNestFactory } from './openapi-nest.factory'; import { OpenApiToolsModule } from './openapi-tools.module'; import { OpenApiService } from './openapi.service'; +import { SwaggerModule } from '@nestjs/swagger'; describe('', () => { + afterEach(() => { + jest.clearAllMocks(); + jest.restoreAllMocks(); + }); + it('should configure OpenApiToolsModule and create an instance of it', async () => { // arrange const app = { @@ -45,4 +51,48 @@ describe('', () => { ); expect(openApiToolsModule.get).toHaveBeenCalledWith(OpenApiService); }); + + it('should call setup method of SwaggerModule with provided custom options', async () => { + const app = { + get: jest.fn(), + }; + const documentBuilder = { + build: jest.fn().mockReturnValue({ info: { title: 'My API' } }), + }; + + const swaggerOptions = {}; + + const swaggerModuleSetupSpy = jest + .spyOn(SwaggerModule, 'setup') + .mockReturnValue({} as any); + + jest.spyOn(SwaggerModule, 'createDocument').mockReturnValue({} as any); + + const webServerPath = 'api/swagger'; + const webServerCustomOptions = { + swaggerOptions: { + oauth2RedirectUrl: 'http://localhost:3000/api/swagger', + }, + }; + + await OpenApiNestFactory.configure( + app as any, + documentBuilder as any, + { + webServerOptions: { + enabled: true, + path: webServerPath, + customOptions: webServerCustomOptions, + }, + }, + swaggerOptions, + ); + + expect(swaggerModuleSetupSpy).toBeCalledWith( + webServerPath, + app, + {}, + webServerCustomOptions, + ); + }); });