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

fix: make getMountedControllers support nested controllers #1804

Merged
merged 4 commits into from
Mar 11, 2022
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
34 changes: 34 additions & 0 deletions packages/platform/common/src/services/Platform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ class MyCtrl {
get() {}
}

@Controller("/my-sub-route")
class MySubCtrl {
@Get("/")
get() {}
}

@Controller({
path: "/my-route",
children: [MySubCtrl]
})
class MyNestedCtrl {
@Get("/")
get() {}
}

describe("Platform", () => {
beforeEach(PlatformTest.create);
afterEach(PlatformTest.reset);
Expand All @@ -37,6 +52,25 @@ describe("Platform", () => {
expect(platform.getMountedControllers()).to.deep.eq([{provider, route: "/test/my-route"}]);
expect(platform.app.use).to.have.been.calledWithExactly("/test/my-route", router.raw);
});
it("should add nested controllers", async () => {
// GIVEN
const nestedProvider: ControllerProvider = PlatformTest.injector.getProvider(MyNestedCtrl)! as ControllerProvider;
const subProvider: ControllerProvider = PlatformTest.injector.getProvider(MySubCtrl)! as ControllerProvider;
const platform = await PlatformTest.get<Platform>(Platform);

sandbox.spy(platform.app, "use");

// WHEN
platform.addRoute("/test", MyNestedCtrl);
const router = PlatformTest.get<PlatformRouter>(nestedProvider.tokenRouter);

// THEN
expect(platform.getMountedControllers()).to.deep.eq([
{provider: nestedProvider, route: "/test/my-route"},
{provider: subProvider, route: "/test/my-route/my-sub-route"}
]);
expect(platform.app.use).to.have.been.calledWithExactly("/test/my-route", router.raw);
});
});
describe("getRoutes", () => {
const sandbox = Sinon.createSandbox();
Expand Down
36 changes: 30 additions & 6 deletions packages/platform/common/src/services/Platform.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Type} from "@tsed/core";
import {Injectable, InjectorService, ProviderScope, ProviderType, TokenProvider} from "@tsed/di";
import {concatPath, EndpointMetadata, getOperationsRoutes, JsonEntityStore} from "@tsed/schema";
import {concatPath, EndpointMetadata, getJsonEntityStore, getOperationsRoutes, JsonEntityStore} from "@tsed/schema";
import {buildRouter, createRouter, getRouter} from "../builder/PlatformControllerBuilder";
import {ControllerProvider} from "../domain/ControllerProvider";
import {PlatformRouteDetails} from "../domain/PlatformRouteDetails";
Expand Down Expand Up @@ -46,12 +47,9 @@ export class Platform {
return;
}

const ctrlPath = concatPath(basePath, JsonEntityStore.from(provider.token).path);
this._controllers.push(...this.getAllControllers(basePath, token));

this._controllers.push({
route: ctrlPath,
provider
});
const ctrlPath = concatPath(basePath, JsonEntityStore.from(provider.token).path);

this.app.use(ctrlPath, ...[].concat(getRouter(injector, provider).callback()));

Expand Down Expand Up @@ -83,6 +81,10 @@ export class Platform {
return this._routes;
}

/**
* Get all controllers mounted on the application.
* @returns {RouteController[]}
*/
public getMountedControllers(): RouteController[] {
return this._controllers;
}
Expand All @@ -103,6 +105,28 @@ export class Platform {
});
}

/**
* Get all router controllers from the controller token.
* @private
*/
private getAllControllers(basePath: string, token: Type<any> | any): RouteController[] {
weirongxu marked this conversation as resolved.
Show resolved Hide resolved
const store: JsonEntityStore = token.isStore ? token : getJsonEntityStore(token);
const ctrlPath = concatPath(basePath, JsonEntityStore.from(token).path);
const controllers: RouteController[] = [
{
route: ctrlPath,
provider: this.injector.getProvider(token) as ControllerProvider
}
];

const children = store.get<Type[]>("childrenControllers", []);

return children.reduce<RouteController[]>((controllers, token) => {
const childBasePath = concatPath(basePath, store.path);
return controllers.concat(this.getAllControllers(childBasePath, token));
}, controllers);
}

/**
* Create controllers from DI
* @private
Expand Down
3 changes: 2 additions & 1 deletion packages/specs/schema/src/utils/getOperationsRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {getOperationsStores} from "./getOperationsStores";
import {JsonOperationRoute} from "../domain/JsonOperationRoute";
import {JsonEntityStore} from "../domain/JsonEntityStore";
import {getJsonEntityStore} from "./getJsonEntityStore";
import {concatPath} from "./concatPath";

export interface GetOperationsRoutesOptions {
withChildren?: boolean;
Expand All @@ -15,7 +16,7 @@ export function getOperationsRoutes<Entity extends JsonMethodStore = JsonMethodS
options: GetOperationsRoutesOptions = {}
): JsonOperationRoute<Entity>[] {
const store: JsonEntityStore = token.isStore ? token : getJsonEntityStore(token);
const basePath = ((options.basePath || "") + (store.path || "")).replace(/\/\//gi, "/");
const basePath = concatPath(options.basePath, store.path);
let operationsRoutes: JsonOperationRoute<Entity>[] = [];

if (options.withChildren) {
Expand Down
26 changes: 1 addition & 25 deletions packages/specs/swagger/src/services/SwaggerService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export class SwaggerService {

this.platform.getMountedControllers().forEach(({route, provider}) => {
if (includeRoute(route, provider, conf)) {
const spec = this.buildRoutes(provider, {
const spec = getSpec(provider.token, {
...options,
rootPath: route.replace(provider.path, "")
});
Expand Down Expand Up @@ -94,28 +94,4 @@ export class SwaggerService {

return {};
}

/**
*
* @param ctrl
* @param options
*/
protected buildRoutes(ctrl: ControllerProvider, options: SpecSerializerOptions) {
const rootPath = options.rootPath + ctrl.path;

ctrl.children
.map((ctrl) => this.injectorService.getProvider(ctrl))
.forEach((provider: ControllerProvider) => {
if (!provider.store.get("hidden")) {
const spec = this.buildRoutes(provider, {
...options,
rootPath
});

options.append(spec);
}
});

return getSpec(ctrl.token, options);
}
}
6 changes: 3 additions & 3 deletions packages/specs/swagger/test/swagger.integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,10 @@ describe("Swagger integration", () => {
swagger: "2.0",
tags: [
{
name: "EventCtrl"
name: "CalendarsController"
},
{
name: "CalendarsController"
name: "EventCtrl"
}
]
});
Expand Down Expand Up @@ -326,7 +326,7 @@ describe("Swagger integration", () => {
}
}
},
tags: [{name: "EventCtrl"}, {name: "CalendarsController"}],
tags: [{name: "CalendarsController"}, {name: "EventCtrl"}],
components: {
schemas: {
Calendar: {
Expand Down
8 changes: 4 additions & 4 deletions packages/specs/swagger/test/swagger.operationId.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ describe("Swagger integration", () => {
swagger: "2.0",
tags: [
{
name: "EventCtrl"
name: "CalendarsController"
},
{
name: "CalendarsController"
name: "EventCtrl"
}
]
});
Expand Down Expand Up @@ -338,10 +338,10 @@ describe("Swagger integration", () => {
},
tags: [
{
name: "EventCtrl"
name: "CalendarsController"
},
{
name: "CalendarsController"
name: "EventCtrl"
}
]
});
Expand Down