From da8ebded25daf25c9cd1365f2a64411048464e6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 7 Nov 2024 12:01:18 +0100 Subject: [PATCH 1/2] fix(microservices): use instance ref to call handler #13473 --- .../listener-metadata-explorer.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/microservices/listener-metadata-explorer.ts b/packages/microservices/listener-metadata-explorer.ts index faa98b71bde..721c2367062 100644 --- a/packages/microservices/listener-metadata-explorer.ts +++ b/packages/microservices/listener-metadata-explorer.ts @@ -39,25 +39,36 @@ export class ListenerMetadataExplorer { const instancePrototype = Object.getPrototypeOf(instance); return this.metadataScanner .getAllMethodNames(instancePrototype) - .map(method => this.exploreMethodMetadata(instancePrototype, method)) + .map(method => + this.exploreMethodMetadata(instance, instancePrototype, method), + ) .filter(metadata => metadata); } public exploreMethodMetadata( + instance: Controller, instancePrototype: object, methodKey: string, ): EventOrMessageListenerDefinition { - const targetCallback = instancePrototype[methodKey]; + const prototypeCallback = instancePrototype[methodKey]; const handlerType = Reflect.getMetadata( PATTERN_HANDLER_METADATA, - targetCallback, + prototypeCallback, ); if (isUndefined(handlerType)) { return; } - const patterns = Reflect.getMetadata(PATTERN_METADATA, targetCallback); - const transport = Reflect.getMetadata(TRANSPORT_METADATA, targetCallback); - const extras = Reflect.getMetadata(PATTERN_EXTRAS_METADATA, targetCallback); + const patterns = Reflect.getMetadata(PATTERN_METADATA, prototypeCallback); + const transport = Reflect.getMetadata( + TRANSPORT_METADATA, + prototypeCallback, + ); + const extras = Reflect.getMetadata( + PATTERN_EXTRAS_METADATA, + prototypeCallback, + ); + + const targetCallback = instance[methodKey]; return { methodKey, targetCallback, From f27304159487ba0f7051e5ad29527eb299599dcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20My=C5=9Bliwiec?= Date: Thu, 7 Nov 2024 12:07:59 +0100 Subject: [PATCH 2/2] test: update unit tests --- .../microservices/test/listeners-metadata-explorer.spec.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/microservices/test/listeners-metadata-explorer.spec.ts b/packages/microservices/test/listeners-metadata-explorer.spec.ts index 18ccdc2f7c9..b4c5c10e2a4 100644 --- a/packages/microservices/test/listeners-metadata-explorer.spec.ts +++ b/packages/microservices/test/listeners-metadata-explorer.spec.ts @@ -71,6 +71,7 @@ describe('ListenerMetadataExplorer', () => { }); it(`should return undefined when "handlerType" metadata is undefined`, () => { const metadata = instance.exploreMethodMetadata( + test, Object.getPrototypeOf(test), 'noPattern', ); @@ -80,6 +81,7 @@ describe('ListenerMetadataExplorer', () => { describe('@MessagePattern', () => { it(`should return pattern properties when "handlerType" metadata is not undefined`, () => { const metadata = instance.exploreMethodMetadata( + test, Object.getPrototypeOf(test), 'testMessage', ); @@ -96,6 +98,7 @@ describe('ListenerMetadataExplorer', () => { }); it(`should return multiple patterns when more than one is declared`, () => { const metadata = instance.exploreMethodMetadata( + test, Object.getPrototypeOf(test), 'testMultipleMessage', ); @@ -116,6 +119,7 @@ describe('ListenerMetadataExplorer', () => { describe('@EventPattern', () => { it(`should return pattern properties when "handlerType" metadata is not undefined`, () => { const metadata = instance.exploreMethodMetadata( + test, Object.getPrototypeOf(test), 'testEvent', ); @@ -132,6 +136,7 @@ describe('ListenerMetadataExplorer', () => { }); it(`should return multiple patterns when more than one is declared`, () => { const metadata = instance.exploreMethodMetadata( + test, Object.getPrototypeOf(test), 'testMultipleEvent', );