Skip to content

fix: createActions should not garble baseclass metadata #1127

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

Merged
merged 3 commits into from
Apr 27, 2023
Merged
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
41 changes: 16 additions & 25 deletions src/metadata-builder/MetadataBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}

/**
Expand Down
14 changes: 10 additions & 4 deletions test/unit/controller-inheritance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ describe('controller inheritance', () => {
@Controller(`/derivative`)
class DerivativeController extends AbstractControllerTemplate {}

@Controller(`/derivative2`)
class DerivativeController2 extends AbstractControllerTemplate {}

@Controller(`/autonomous`)
class AutonomousController {
@Post()
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it intentional that the abstract controller is registered too? Seems like it wasn't the case before

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is; the actions in the storage are actually the base data (ActionMetadataArgs) and are shared amongst all deriving classes. If it were altered (as a previous change introduced), only the first derived class 'gets' the actions; the action from the storage is used to create the ActualMetadata in createActions(), and only at that place, the controller.target is filled in as target for the action (which is not the action metadata that is subject of this unit test).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, you are right. Seems like we had an incorrect reference mutation, the fix was just obscured a bit by the structure change :).

Thanks for the contribution!

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);
Expand Down