diff --git a/src/metadata-builder/MetadataBuilder.ts b/src/metadata-builder/MetadataBuilder.ts index 53d710f7..ccdda5f2 100644 --- a/src/metadata-builder/MetadataBuilder.ts +++ b/src/metadata-builder/MetadataBuilder.ts @@ -8,7 +8,6 @@ import { ResponseHandlerMetadata } from '../metadata/ResponseHandleMetadata'; import { RoutingControllersOptions } from '../RoutingControllersOptions'; import { UseMetadata } from '../metadata/UseMetadata'; import { getMetadataArgsStorage } from '../index'; -import { ActionMetadataArgs } from '../metadata/args/ActionMetadataArgs'; /** * Builds metadata from the given metadata arguments. @@ -92,33 +91,25 @@ export class MetadataBuilder { * Creates action metadatas. */ protected createActions(controller: ControllerMetadata): ActionMetadata[] { - let target = controller.target; - const actionsWithTarget: ActionMetadataArgs[] = []; - while (target) { - const actions = getMetadataArgsStorage() - .filterActionsWithTarget(target) - .filter(action => { - return actionsWithTarget.map(a => a.method).indexOf(action.method) === -1; + const actionsWithTarget: ActionMetadata[] = []; + for (let target = controller.target; target; target = Object.getPrototypeOf(target)) { + const actions = getMetadataArgsStorage().filterActionsWithTarget(target); + const methods = actionsWithTarget.map(a => a.method); + actions + .filter(({ method }) => !methods.includes(method)) + .forEach(actionArgs => { + const action = new ActionMetadata(controller, { ...actionArgs, target: controller.target }, this.options); + action.options = { ...controller.options, ...actionArgs.options }; + action.params = this.createParams(action); + action.uses = this.createActionUses(action); + action.interceptors = this.createActionInterceptorUses(action); + action.build(this.createActionResponseHandlers(action)); + + actionsWithTarget.push(action); }); - - actions.forEach(a => { - a.target = controller.target; - - actionsWithTarget.push(a); - }); - - target = Object.getPrototypeOf(target); } - return actionsWithTarget.map(actionArgs => { - const action = new ActionMetadata(controller, actionArgs, this.options); - action.options = { ...controller.options, ...actionArgs.options }; - action.params = this.createParams(action); - action.uses = this.createActionUses(action); - action.interceptors = this.createActionInterceptorUses(action); - action.build(this.createActionResponseHandlers(action)); - return action; - }); + return actionsWithTarget; } /** diff --git a/test/unit/controller-inheritance.spec.ts b/test/unit/controller-inheritance.spec.ts index c4755442..4c246c1a 100644 --- a/test/unit/controller-inheritance.spec.ts +++ b/test/unit/controller-inheritance.spec.ts @@ -26,6 +26,9 @@ describe('controller inheritance', () => { @Controller(`/derivative`) class DerivativeController extends AbstractControllerTemplate {} + @Controller(`/derivative2`) + class DerivativeController2 extends AbstractControllerTemplate {} + @Controller(`/autonomous`) class AutonomousController { @Post() @@ -39,9 +42,12 @@ describe('controller inheritance', () => { expect(storage.controllers[0].target).to.be.eq(DerivativeController); expect(storage.controllers[0].route).to.be.eq('/derivative'); - expect(storage.controllers[1].target).to.be.eq(AutonomousController); - expect(storage.controllers[1].route).to.be.eq('/autonomous'); - expect(storage.actions[0].target).to.be.eq(DerivativeController); + expect(storage.controllers[1].target).to.be.eq(DerivativeController2); + expect(storage.controllers[1].route).to.be.eq('/derivative2'); + expect(storage.controllers[2].target).to.be.eq(AutonomousController); + expect(storage.controllers[2].route).to.be.eq('/autonomous'); + + expect(storage.actions[0].target).to.be.eq(AbstractControllerTemplate); expect(storage.actions[0].type).to.be.eq('post'); expect(storage.actions[0].method).to.be.eq('create'); expect(storage.actions[1].target).to.be.eq(AutonomousController); @@ -125,7 +131,7 @@ describe('controller inheritance', () => { expect(storage.controllers[0].route).to.be.eq('/derivative'); expect(storage.controllers[1].target).to.be.eq(AutonomousController); expect(storage.controllers[1].route).to.be.eq('/autonomous'); - expect(storage.actions[0].target).to.be.eq(DerivativeController); + expect(storage.actions[0].target).to.be.eq(AbstractControllerTemplate); expect(storage.actions[0].type).to.be.eq('post'); expect(storage.actions[0].method).to.be.eq('create'); expect(storage.actions[1].target).to.be.eq(AutonomousController);