Closed mjpowersjr closed 3 years ago
Hi @mjpowersjr !
Thanks for opening the issue, is there a minimum reproduction you could provide to help us fix this? Or better yet, would you like to contribute a PR?
Thanks for the quick response. I did take a look at the code, but TBH I'm a bit out of my depth on the opentelemetry side. This is a simple test that could be dropped along side your current tests.
test('should preserve this binding in handler using wrapRoutes', async ({ is, same, teardown }) => {
class TestController {
async handlerRequest(request, reply) {
if (! this) {
throw new Error('this is undefined');
}
return Object.keys(this)
}
}
// Note: fastify does not support binding 'this' with arrow functions based router handlers!
const controller = new TestController();
const fastify = setupTest({
serviceName: 'test',
wrapRoutes: true,
}, controller.handlerRequest);
const reply = await fastify.inject(injectArgs);
is(reply.statusCode, 200);
teardown(() => {
fastify.close()
})
});
Thanks for the test snippet @mjpowersjr! I was able to track down the bug and have a fix in PR 👍
Fix is live in v0.11.2 🚀
Thanks you!
Fastify's default behavior is to bind the
this
variable to the Fastify Server instance. (see Route Options).When using this plugin's
wrapRoutes
option,this
is set toundefined
when the route handler is invoked. Ideally, the original behavior could be maintained, while using wrapRoutes.