diff --git a/packages/core/middleware/routes-mapper.ts b/packages/core/middleware/routes-mapper.ts index 43946862818..ae27962d199 100644 --- a/packages/core/middleware/routes-mapper.ts +++ b/packages/core/middleware/routes-mapper.ts @@ -28,8 +28,11 @@ export class RoutesMapper { }, ]; } - const routePath: string = Reflect.getMetadata(PATH_METADATA, route); - if (this.isRouteInfo(routePath, route)) { + const routePathOrPaths: string | string[] = Reflect.getMetadata( + PATH_METADATA, + route, + ); + if (this.isRouteInfo(routePathOrPaths, route)) { return [ { path: this.validateRoutePath(route.path), @@ -43,21 +46,28 @@ export class RoutesMapper { ); const concatPaths = (acc: T[], currentValue: T[]) => acc.concat(currentValue); - return paths - .map( - item => - item.path && - item.path.map(p => ({ - path: - this.validateGlobalPath(routePath) + this.validateRoutePath(p), - method: item.requestMethod, - })), + + return [] + .concat(routePathOrPaths) + .map(routePath => + paths + .map( + item => + item.path && + item.path.map(p => ({ + path: + this.validateGlobalPath(routePath) + + this.validateRoutePath(p), + method: item.requestMethod, + })), + ) + .reduce(concatPaths, []), ) .reduce(concatPaths, []); } private isRouteInfo( - path: string | undefined, + path: string | string[] | undefined, objectOrClass: Function | RouteInfo, ): objectOrClass is RouteInfo { return isUndefined(path); diff --git a/packages/core/test/middleware/routes-mapper.spec.ts b/packages/core/test/middleware/routes-mapper.spec.ts index 3cf4b5aa0a3..7c1aabd5174 100644 --- a/packages/core/test/middleware/routes-mapper.spec.ts +++ b/packages/core/test/middleware/routes-mapper.spec.ts @@ -34,4 +34,32 @@ describe('RoutesMapper', () => { { path: '/test/another', method: RequestMethod.DELETE }, ]); }); + @Controller(['test', 'test2']) + class TestRouteWithMultiplePaths { + @RequestMapping({ path: 'test' }) + public getTest() {} + + @RequestMapping({ path: 'another', method: RequestMethod.DELETE }) + public getAnother() {} + } + + it('should map a controller with multiple paths to "ControllerMetadata" in forRoutes', () => { + const config = { + middleware: 'Test', + forRoutes: [ + { path: 'test', method: RequestMethod.GET }, + TestRouteWithMultiplePaths, + ], + }; + + expect(mapper.mapRouteToRouteInfo(config.forRoutes[0])).to.deep.equal([ + { path: '/test', method: RequestMethod.GET }, + ]); + expect(mapper.mapRouteToRouteInfo(config.forRoutes[1])).to.deep.equal([ + { path: '/test/test', method: RequestMethod.GET }, + { path: '/test/another', method: RequestMethod.DELETE }, + { path: '/test2/test', method: RequestMethod.GET }, + { path: '/test2/another', method: RequestMethod.DELETE }, + ]); + }); });