Skip to content

Commit

Permalink
Merge pull request #14112 from nestjs/fix/instance-method-call
Browse files Browse the repository at this point in the history
fix(microservices): use instance refs for target handler callbacks
  • Loading branch information
kamilmysliwiec authored Nov 8, 2024
2 parents 84bf570 + f273041 commit f191f0f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
23 changes: 17 additions & 6 deletions packages/microservices/listener-metadata-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ describe('ListenerMetadataExplorer', () => {
});
it(`should return undefined when "handlerType" metadata is undefined`, () => {
const metadata = instance.exploreMethodMetadata(
test,
Object.getPrototypeOf(test),
'noPattern',
);
Expand All @@ -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',
);
Expand All @@ -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',
);
Expand All @@ -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',
);
Expand All @@ -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',
);
Expand Down

0 comments on commit f191f0f

Please sign in to comment.