diff --git a/plugin/controller/test/fixtures/apps/controller-app/app/controller/MiddlewareController.ts b/plugin/controller/test/fixtures/apps/controller-app/app/controller/MiddlewareController.ts index d044053b..0d26292a 100644 --- a/plugin/controller/test/fixtures/apps/controller-app/app/controller/MiddlewareController.ts +++ b/plugin/controller/test/fixtures/apps/controller-app/app/controller/MiddlewareController.ts @@ -8,6 +8,7 @@ import { import AppService from '../../modules/multi-module-service/AppService'; import { countMw } from '../middleware/count_mw'; import { logMwFactory } from '../middleware/log_mw'; +import { callModuleCtx } from '../middleware/call_module'; @HTTPController({ path: '/middleware', @@ -33,4 +34,13 @@ export class MiddlewareController { async middleware() { return {}; } + + @HTTPMethod({ + method: HTTPMethodEnum.GET, + path: '/methodCallModule', + }) + @Middleware(callModuleCtx) + async middlewareCallModule() { + return {}; + } } diff --git a/plugin/controller/test/fixtures/apps/controller-app/app/middleware/call_module.ts b/plugin/controller/test/fixtures/apps/controller-app/app/middleware/call_module.ts new file mode 100644 index 00000000..fc6d5c00 --- /dev/null +++ b/plugin/controller/test/fixtures/apps/controller-app/app/middleware/call_module.ts @@ -0,0 +1,7 @@ +import { Context } from 'egg'; +import { Next } from '@eggjs/tegg'; + +export async function callModuleCtx(ctx: Context, next: Next) { + await (ctx.module as any).multiModuleService.appService.findApp('foo'); + await next(); +} diff --git a/plugin/controller/test/http/middleware.test.ts b/plugin/controller/test/http/middleware.test.ts index e79b569c..42b482c5 100644 --- a/plugin/controller/test/http/middleware.test.ts +++ b/plugin/controller/test/http/middleware.test.ts @@ -48,4 +48,11 @@ describe('test/middleware.test.ts', () => { assert(res.body.log === 'use middleware'); }); }); + + it('method middleware call module should work', async () => { + app.mockCsrf(); + await app.httpRequest() + .get('/middleware/methodCallModule') + .expect(200); + }); }); diff --git a/plugin/tegg/lib/EggContextCompatibleHook.ts b/plugin/tegg/lib/EggContextCompatibleHook.ts index 497019ee..34ae55c3 100644 --- a/plugin/tegg/lib/EggContextCompatibleHook.ts +++ b/plugin/tegg/lib/EggContextCompatibleHook.ts @@ -35,4 +35,12 @@ export class EggContextCompatibleHook implements LifecycleHook { + const rootProto = ctx.get(ROOT_PROTO); + if (rootProto) { + // Ensure ContextInitiator be called. + await EggContainerFactory.getOrCreateEggObject(rootProto as EggPrototype); + } + } }