Skip to content
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

refactor(core): Auto-register controllers at startup (no-changelog) #9781

Merged
merged 5 commits into from
Jun 19, 2024

Conversation

netroy
Copy link
Member

@netroy netroy commented Jun 17, 2024

Summary

Extracted out of #9734

This PR:

  1. Makes controller registration automatic by auto-registering all classes marked with the RestController decorator. All we now need to do is to ensure that the relevant controller classes are imported somewhere in the dependency tree.
  2. Switches to using more type-safe code instead of Reflect.getMetadata
  3. Prevents us from adding multiple routes to the same handle
  4. Adds unit tests for controller registration code

Review / Merge checklist

  • PR title and summary are descriptive
  • Tests included

also stop using type-unsafe `Reflect.getMetadata`
@n8n-assistant n8n-assistant bot added core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team labels Jun 17, 2024
tomi
tomi previously approved these changes Jun 18, 2024
Copy link
Contributor

@tomi tomi left a comment

Choose a reason for hiding this comment

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

Nice job 👌 Couple minor comments & questions, nothing that would prevent from merging

Comment on lines +44 to +45
route = {} as RouteMetadata;
metadata.routes.set(handlerName, route);
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this break the type safety, if we can't rely anymore that the objects in the route metadata conform to that type? Should we instead validate in the activateController that the metadata has been defined correctly and throw if not?

Copy link
Member Author

Choose a reason for hiding this comment

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

Doesn't this break the type safety

it kinda does. but If someone creates a new controller class that does not use the decorators correctly, the application will already fail to start, so I'm not that worried about it. But I can also add some additional validation in activateController, to add more explicit errors.

Copy link
Member Author

Choose a reason for hiding this comment

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

adding the validation seems pretty straight forward, but adding tests for it require resetting the registry, which is making the code a bit more complicated. I'd like to try doing this in another PR.

packages/cli/src/decorators/controller.registry.ts Outdated Show resolved Hide resolved
packages/cli/src/Server.ts Outdated Show resolved Hide resolved
packages/cli/src/decorators/Licensed.ts Show resolved Hide resolved
Copy link
Contributor

✅ No visual regressions found.

Copy link
Contributor

✅ All Cypress E2E specs passed

Copy link

cypress bot commented Jun 18, 2024

2 flaky tests on run #5572 ↗︎

0 395 0 0 Flakiness 2

Details:

🌳 🖥️ browsers:node18.12.0-chrome107 🤖 netroy 🗃️ e2e/*
Project: n8n Commit: 114765135d
Status: Passed Duration: 04:19 💡
Started: Jun 19, 2024 7:41 AM Ended: Jun 19, 2024 7:46 AM
Flakiness  10-undo-redo.cy.ts • 1 flaky test

View Output Video

Test Artifacts
Undo/Redo > should undo/redo adding connected nodes Test Replay Screenshots Video
Flakiness  12-canvas.cy.ts • 1 flaky test

View Output Video

Test Artifacts
Canvas Node Manipulation and Navigation > should preserve connections after rename & node-view switch Test Replay Screenshots Video

Review all test suite changes for PR #9781 ↗︎

packages/cli/src/Server.ts Outdated Show resolved Hide resolved
packages/cli/src/Server.ts Outdated Show resolved Hide resolved
packages/cli/src/decorators/controller.registry.ts Outdated Show resolved Hide resolved
packages/cli/src/decorators/controller.registry.ts Outdated Show resolved Hide resolved
@netroy netroy requested review from ivov and tomi June 18, 2024 11:53
Copy link
Contributor

@tomi tomi left a comment

Choose a reason for hiding this comment

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

🚀 Just one question

Comment on lines 204 to 222

// ----------------------------------------
// SAML
// ----------------------------------------
await this.registerAdditionalControllers();

// initialize SamlService if it is licensed, even if not enabled, to
// set up the initial environment
try {
await Container.get(SamlService).init();
} catch (error) {
this.logger.warn(`SAML initialization failed: ${error.message}`);
}

// ----------------------------------------
// Source Control
// ----------------------------------------
try {
await Container.get(SourceControlService).init();
} catch (error) {
this.logger.warn(`Source Control initialization failed: ${error.message}`);
}
// register all known controllers
Container.get(ControllerRegistry).activate(app);
Copy link
Contributor

Choose a reason for hiding this comment

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

The order in which these are registered has been inversed here. Is that intentional or does it matter?

Copy link
Member Author

@netroy netroy Jun 18, 2024

Choose a reason for hiding this comment

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

here the controllers used to be registered before the init on the related service had been called, which likely made some of those endpoint 5xx for a short while after startup. This change is meant to ensure that all relevant services have been initialized before we setup any of the routers.

Copy link
Contributor

✅ All Cypress E2E specs passed

Copy link
Contributor

@ivov ivov left a comment

Choose a reason for hiding this comment

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

Lovely work!

Copy link
Contributor

✅ All Cypress E2E specs passed

@netroy netroy merged commit 3b70330 into master Jun 19, 2024
53 of 54 checks passed
@netroy netroy deleted the refactor-controller-registration branch June 19, 2024 07:57
@janober
Copy link
Member

janober commented Jun 20, 2024

Got released with [email protected]

1 similar comment
@janober
Copy link
Member

janober commented Jun 20, 2024

Got released with [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Enhancement outside /nodes-base and /editor-ui n8n team Authored by the n8n team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants