From 9b475285274765430af6e2a91ba5e3d62868e5e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Poullain?= Date: Thu, 7 Apr 2022 19:04:16 +0200 Subject: [PATCH] Support Swagger security, docs and servers in sub-controllers --- .../routes/make-controller-routes.spec.ts | 46 ++++++++++++++++++- .../src/core/routes/make-controller-routes.ts | 2 +- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/core/src/core/routes/make-controller-routes.spec.ts b/packages/core/src/core/routes/make-controller-routes.spec.ts index 683d91ecec..2341c56f5f 100644 --- a/packages/core/src/core/routes/make-controller-routes.spec.ts +++ b/packages/core/src/core/routes/make-controller-routes.spec.ts @@ -887,7 +887,51 @@ describe('makeControllerRoutes', () => { } as IApiPaths); }); - it('but with not the root servers, security requirements and externalDocs in the paths.', () => { + it('with the sub-controllers\' servers, security requirements and externalDocs in the paths.', () => { + const server: IApiServer = { url: 'http://example.com' }; + const externalDocs: IApiExternalDocumentation = { url: 'http://example.com/docs' }; + const securityRequirement: IApiSecurityRequirement = { a: [ 'b' ] }; + + @ApiServer(server) + @ApiExternalDoc(externalDocs) + @ApiSecurityRequirement(securityRequirement) + class UserController { + @Get('/foo') + foo(){} + } + + @ApiInfo(infoMetadata) + class ApiController { + subControllers = [ + controller('/users', UserController), + ] + } + + class AppController { + subControllers = [ + controller('/api', ApiController) + ]; + } + + Array.from(makeControllerRoutes(AppController, services)); + const document = openApi.getDocument(ApiController); + strictEqual(document.servers, undefined); + strictEqual(document.externalDocs, undefined); + strictEqual(document.security, undefined); + + deepStrictEqual(document.paths, { + '/users/foo': { + get: { + externalDocs, + responses: {}, + security: [ securityRequirement ], + servers: [ server ], + } + } + }); + }); + + it('but without the root servers, security requirements and externalDocs in the paths.', () => { const server: IApiServer = { url: 'http://example.com' }; const externalDocs: IApiExternalDocumentation = { url: 'http://example.com/docs' }; const securityRequirement: IApiSecurityRequirement = { a: [ 'b' ] }; diff --git a/packages/core/src/core/routes/make-controller-routes.ts b/packages/core/src/core/routes/make-controller-routes.ts index 4679740c97..4452b888e7 100644 --- a/packages/core/src/core/routes/make-controller-routes.ts +++ b/packages/core/src/core/routes/make-controller-routes.ts @@ -95,7 +95,7 @@ export function* makeControllerRoutes( const controllerComponents = openapi ? getApiComponents(controllerClass, controller) : {}; const controllerOperation = openapi ? getApiCompleteOperation(controllerClass, controller) : { responses: {} }; const controllerTags = openapi ? getApiTags(controllerClass) : undefined; - if (openapi) { + if (openapi && info) { delete controllerOperation.servers; delete controllerOperation.externalDocs; delete controllerOperation.security;