Skip to content

Commit 126719a

Browse files
authored
fix: createActions should not mutate actions in metadata storage (#1127)
1 parent 126eb12 commit 126719a

File tree

2 files changed

+26
-29
lines changed

2 files changed

+26
-29
lines changed

src/metadata-builder/MetadataBuilder.ts

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import { ResponseHandlerMetadata } from '../metadata/ResponseHandleMetadata';
88
import { RoutingControllersOptions } from '../RoutingControllersOptions';
99
import { UseMetadata } from '../metadata/UseMetadata';
1010
import { getMetadataArgsStorage } from '../index';
11-
import { ActionMetadataArgs } from '../metadata/args/ActionMetadataArgs';
1211

1312
/**
1413
* Builds metadata from the given metadata arguments.
@@ -92,33 +91,25 @@ export class MetadataBuilder {
9291
* Creates action metadatas.
9392
*/
9493
protected createActions(controller: ControllerMetadata): ActionMetadata[] {
95-
let target = controller.target;
96-
const actionsWithTarget: ActionMetadataArgs[] = [];
97-
while (target) {
98-
const actions = getMetadataArgsStorage()
99-
.filterActionsWithTarget(target)
100-
.filter(action => {
101-
return actionsWithTarget.map(a => a.method).indexOf(action.method) === -1;
94+
const actionsWithTarget: ActionMetadata[] = [];
95+
for (let target = controller.target; target; target = Object.getPrototypeOf(target)) {
96+
const actions = getMetadataArgsStorage().filterActionsWithTarget(target);
97+
const methods = actionsWithTarget.map(a => a.method);
98+
actions
99+
.filter(({ method }) => !methods.includes(method))
100+
.forEach(actionArgs => {
101+
const action = new ActionMetadata(controller, { ...actionArgs, target: controller.target }, this.options);
102+
action.options = { ...controller.options, ...actionArgs.options };
103+
action.params = this.createParams(action);
104+
action.uses = this.createActionUses(action);
105+
action.interceptors = this.createActionInterceptorUses(action);
106+
action.build(this.createActionResponseHandlers(action));
107+
108+
actionsWithTarget.push(action);
102109
});
103-
104-
actions.forEach(a => {
105-
a.target = controller.target;
106-
107-
actionsWithTarget.push(a);
108-
});
109-
110-
target = Object.getPrototypeOf(target);
111110
}
112111

113-
return actionsWithTarget.map(actionArgs => {
114-
const action = new ActionMetadata(controller, actionArgs, this.options);
115-
action.options = { ...controller.options, ...actionArgs.options };
116-
action.params = this.createParams(action);
117-
action.uses = this.createActionUses(action);
118-
action.interceptors = this.createActionInterceptorUses(action);
119-
action.build(this.createActionResponseHandlers(action));
120-
return action;
121-
});
112+
return actionsWithTarget;
122113
}
123114

124115
/**

test/unit/controller-inheritance.spec.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ describe('controller inheritance', () => {
2626
@Controller(`/derivative`)
2727
class DerivativeController extends AbstractControllerTemplate {}
2828

29+
@Controller(`/derivative2`)
30+
class DerivativeController2 extends AbstractControllerTemplate {}
31+
2932
@Controller(`/autonomous`)
3033
class AutonomousController {
3134
@Post()
@@ -39,9 +42,12 @@ describe('controller inheritance', () => {
3942

4043
expect(storage.controllers[0].target).to.be.eq(DerivativeController);
4144
expect(storage.controllers[0].route).to.be.eq('/derivative');
42-
expect(storage.controllers[1].target).to.be.eq(AutonomousController);
43-
expect(storage.controllers[1].route).to.be.eq('/autonomous');
44-
expect(storage.actions[0].target).to.be.eq(DerivativeController);
45+
expect(storage.controllers[1].target).to.be.eq(DerivativeController2);
46+
expect(storage.controllers[1].route).to.be.eq('/derivative2');
47+
expect(storage.controllers[2].target).to.be.eq(AutonomousController);
48+
expect(storage.controllers[2].route).to.be.eq('/autonomous');
49+
50+
expect(storage.actions[0].target).to.be.eq(AbstractControllerTemplate);
4551
expect(storage.actions[0].type).to.be.eq('post');
4652
expect(storage.actions[0].method).to.be.eq('create');
4753
expect(storage.actions[1].target).to.be.eq(AutonomousController);
@@ -125,7 +131,7 @@ describe('controller inheritance', () => {
125131
expect(storage.controllers[0].route).to.be.eq('/derivative');
126132
expect(storage.controllers[1].target).to.be.eq(AutonomousController);
127133
expect(storage.controllers[1].route).to.be.eq('/autonomous');
128-
expect(storage.actions[0].target).to.be.eq(DerivativeController);
134+
expect(storage.actions[0].target).to.be.eq(AbstractControllerTemplate);
129135
expect(storage.actions[0].type).to.be.eq('post');
130136
expect(storage.actions[0].method).to.be.eq('create');
131137
expect(storage.actions[1].target).to.be.eq(AutonomousController);

0 commit comments

Comments
 (0)