Skip to content

fix: Controller inheritance Bugfix #736 #737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/metadata-builder/MetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,21 @@ export class MetadataBuilder {
let target = controller.target;
const actionsWithTarget: ActionMetadataArgs[] = [];
while (target) {
actionsWithTarget.push(
...getMetadataArgsStorage()
.filterActionsWithTarget(target)
.filter(action => {
return actionsWithTarget.map(a => a.method).indexOf(action.method) === -1;
})
);
const actions = getMetadataArgsStorage()
.filterActionsWithTarget(target)
.filter(action => {
return actionsWithTarget.map(a => a.method).indexOf(action.method) === -1;
});

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 };
Expand Down
36 changes: 36 additions & 0 deletions test/unit/controller-inheritance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ describe('controller inheritance', () => {
// Build controllers
const metadataBuilder = new MetadataBuilder({});
const meta = metadataBuilder.buildControllerMetadata([DerivativeController]);
const storage = getMetadataArgsStorage();

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.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);
expect(storage.actions[1].type).to.be.eq('post');
expect(storage.actions[1].method).to.be.eq('create');

expect(meta.length).to.be.eq(1);
expect(meta[0].route).to.be.eq('/derivative');
Expand Down Expand Up @@ -65,6 +77,18 @@ describe('controller inheritance', () => {
// Build controllers
const metadataBuilder = new MetadataBuilder({});
const meta = metadataBuilder.buildControllerMetadata([AutonomousController]);
const storage = getMetadataArgsStorage();

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(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);
expect(storage.actions[1].type).to.be.eq('post');
expect(storage.actions[1].method).to.be.eq('create');

expect(meta.length).to.be.eq(1);
expect(meta[0].route).to.be.eq('/autonomous');
Expand Down Expand Up @@ -95,6 +119,18 @@ describe('controller inheritance', () => {
// Build controllers
const metadataBuilder = new MetadataBuilder({});
const meta = metadataBuilder.buildControllerMetadata();
const storage = getMetadataArgsStorage();

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.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);
expect(storage.actions[1].type).to.be.eq('post');
expect(storage.actions[1].method).to.be.eq('create');

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