Skip to content

Commit

Permalink
fix(rest): use context.get() to resolve current controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Feb 14, 2018
1 parent baf9928 commit 472d9da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
17 changes: 11 additions & 6 deletions packages/rest/src/router/routing-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
Constructor,
instantiateClass,
invokeMethod,
BindingScope,
} from '@loopback/context';
import {ServerRequest} from 'http';
import * as HttpErrors from 'http-errors';
Expand Down Expand Up @@ -261,6 +262,7 @@ export class Route extends BaseRoute {
type ControllerInstance = {[opName: string]: Function};

export class ControllerRoute extends BaseRoute {
protected readonly _controllerBindingKey: string;
protected readonly _methodName: string;

constructor(
Expand Down Expand Up @@ -298,6 +300,7 @@ export class ControllerRoute extends BaseRoute {
}

this._methodName = methodName;
this._controllerBindingKey = `controllers.${this._controllerCtor.name}`;
}

describe(): string {
Expand All @@ -306,6 +309,12 @@ export class ControllerRoute extends BaseRoute {

updateBindings(requestContext: Context) {
const ctor = this._controllerCtor;
if (!requestContext.contains(this._controllerBindingKey)) {
requestContext
.bind(this._controllerBindingKey)
.toClass(ctor)
.inScope(BindingScope.SINGLETON);
}
requestContext.bind('controller.current.ctor').to(ctor);
requestContext.bind('controller.current.operation').to(this._methodName);
}
Expand All @@ -329,14 +338,10 @@ export class ControllerRoute extends BaseRoute {
);
}

private async _createControllerInstance(
private _createControllerInstance(
requestContext: Context,
): Promise<ControllerInstance> {
const valueOrPromise = instantiateClass(
this._controllerCtor,
requestContext,
);
return (await Promise.resolve(valueOrPromise)) as ControllerInstance;
return requestContext.get(this._controllerBindingKey);
}
}

Expand Down
26 changes: 26 additions & 0 deletions packages/rest/test/acceptance/routing/routing.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,32 @@ describe('Routing', () => {
});
});

it('binds the current controller', async () => {
const app = givenAnApplication();
const server = await givenAServer(app);
const spec = anOpenApiSpec()
.withOperationReturningString('get', '/name', 'checkController')
.build();

@api(spec)
class GetCurrentController {
async checkController(
@inject('controllers.GetCurrentController') inst: GetCurrentController,
): Promise<object> {
return {
result: this === inst,
};
}
}
givenControllerInApp(app, GetCurrentController);

return whenIMakeRequestTo(server)
.get('/name')
.expect({
result: true,
});
});

it('supports function-based routes', async () => {
const app = givenAnApplication();
const server = await givenAServer(app);
Expand Down

0 comments on commit 472d9da

Please sign in to comment.