Skip to content

Commit af9a973

Browse files
authored
Merge pull request #741 from ivanproskuryakov/736-inheritance-bugfix-2
fix: Controller inheritance Bugfix #736
2 parents af0cdf8 + a18cffa commit af9a973

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

src/metadata-builder/MetadataBuilder.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,21 @@ export class MetadataBuilder {
9696
let target = controller.target;
9797
const actionsWithTarget: ActionMetadataArgs[] = [];
9898
while (target) {
99-
actionsWithTarget.push(
100-
...getMetadataArgsStorage()
101-
.filterActionsWithTarget(target)
102-
.filter(action => {
103-
return actionsWithTarget.map(a => a.method).indexOf(action.method) === -1;
104-
})
105-
);
99+
const actions = getMetadataArgsStorage()
100+
.filterActionsWithTarget(target)
101+
.filter(action => {
102+
return actionsWithTarget.map(a => a.method).indexOf(action.method) === -1;
103+
});
104+
105+
actions.forEach(a => {
106+
a.target = controller.target;
107+
108+
actionsWithTarget.push(a);
109+
});
110+
106111
target = Object.getPrototypeOf(target);
107112
}
113+
108114
return actionsWithTarget.map(actionArgs => {
109115
const action = new ActionMetadata(controller, actionArgs, this.options);
110116
action.options = { ...controller.options, ...actionArgs.options };

test/unit/controller-inheritance.spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,18 @@ describe('controller inheritance', () => {
3535
// Build controllers
3636
const metadataBuilder = new MetadataBuilder({});
3737
const meta = metadataBuilder.buildControllerMetadata([DerivativeController]);
38+
const storage = getMetadataArgsStorage();
39+
40+
expect(storage.controllers[0].target).to.be.eq(DerivativeController);
41+
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.actions[0].type).to.be.eq('post');
46+
expect(storage.actions[0].method).to.be.eq('create');
47+
expect(storage.actions[1].target).to.be.eq(AutonomousController);
48+
expect(storage.actions[1].type).to.be.eq('post');
49+
expect(storage.actions[1].method).to.be.eq('create');
3850

3951
expect(meta.length).to.be.eq(1);
4052
expect(meta[0].route).to.be.eq('/derivative');
@@ -65,6 +77,18 @@ describe('controller inheritance', () => {
6577
// Build controllers
6678
const metadataBuilder = new MetadataBuilder({});
6779
const meta = metadataBuilder.buildControllerMetadata([AutonomousController]);
80+
const storage = getMetadataArgsStorage();
81+
82+
expect(storage.controllers[0].target).to.be.eq(DerivativeController);
83+
expect(storage.controllers[0].route).to.be.eq('/derivative');
84+
expect(storage.controllers[1].target).to.be.eq(AutonomousController);
85+
expect(storage.controllers[1].route).to.be.eq('/autonomous');
86+
expect(storage.actions[0].target).to.be.eq(AbstractControllerTemplate);
87+
expect(storage.actions[0].type).to.be.eq('post');
88+
expect(storage.actions[0].method).to.be.eq('create');
89+
expect(storage.actions[1].target).to.be.eq(AutonomousController);
90+
expect(storage.actions[1].type).to.be.eq('post');
91+
expect(storage.actions[1].method).to.be.eq('create');
6892

6993
expect(meta.length).to.be.eq(1);
7094
expect(meta[0].route).to.be.eq('/autonomous');
@@ -95,6 +119,18 @@ describe('controller inheritance', () => {
95119
// Build controllers
96120
const metadataBuilder = new MetadataBuilder({});
97121
const meta = metadataBuilder.buildControllerMetadata();
122+
const storage = getMetadataArgsStorage();
123+
124+
expect(storage.controllers[0].target).to.be.eq(DerivativeController);
125+
expect(storage.controllers[0].route).to.be.eq('/derivative');
126+
expect(storage.controllers[1].target).to.be.eq(AutonomousController);
127+
expect(storage.controllers[1].route).to.be.eq('/autonomous');
128+
expect(storage.actions[0].target).to.be.eq(DerivativeController);
129+
expect(storage.actions[0].type).to.be.eq('post');
130+
expect(storage.actions[0].method).to.be.eq('create');
131+
expect(storage.actions[1].target).to.be.eq(AutonomousController);
132+
expect(storage.actions[1].type).to.be.eq('post');
133+
expect(storage.actions[1].method).to.be.eq('create');
98134

99135
expect(meta.length).to.be.eq(2);
100136
expect(meta[0].actions.length).to.be.eq(1);

0 commit comments

Comments
 (0)