diff --git a/packages/microservices/decorators/event-pattern.decorator.ts b/packages/microservices/decorators/event-pattern.decorator.ts index cf04e1fc7ed..ead1830b911 100644 --- a/packages/microservices/decorators/event-pattern.decorator.ts +++ b/packages/microservices/decorators/event-pattern.decorator.ts @@ -61,7 +61,14 @@ export const EventPattern: { descriptor.value, ); Reflect.defineMetadata(TRANSPORT_METADATA, transport, descriptor.value); - Reflect.defineMetadata(PATTERN_EXTRAS_METADATA, extras, descriptor.value); + Reflect.defineMetadata( + PATTERN_EXTRAS_METADATA, + { + ...Reflect.getMetadata(PATTERN_EXTRAS_METADATA, descriptor.value), + ...extras, + }, + descriptor.value, + ); return descriptor; }; }; diff --git a/packages/microservices/decorators/message-pattern.decorator.ts b/packages/microservices/decorators/message-pattern.decorator.ts index 75773090818..86d460f1f28 100644 --- a/packages/microservices/decorators/message-pattern.decorator.ts +++ b/packages/microservices/decorators/message-pattern.decorator.ts @@ -75,7 +75,14 @@ export const MessagePattern: { descriptor.value, ); Reflect.defineMetadata(TRANSPORT_METADATA, transport, descriptor.value); - Reflect.defineMetadata(PATTERN_EXTRAS_METADATA, extras, descriptor.value); + Reflect.defineMetadata( + PATTERN_EXTRAS_METADATA, + { + ...Reflect.getMetadata(PATTERN_EXTRAS_METADATA, descriptor.value), + ...extras, + }, + descriptor.value, + ); return descriptor; }; }; diff --git a/packages/microservices/test/decorators/event-pattern.decorator.spec.ts b/packages/microservices/test/decorators/event-pattern.decorator.spec.ts index dbfb076969f..3a78e602d2d 100644 --- a/packages/microservices/test/decorators/event-pattern.decorator.spec.ts +++ b/packages/microservices/test/decorators/event-pattern.decorator.spec.ts @@ -54,6 +54,8 @@ describe('@EventPattern', () => { }); describe('decorator overloads', () => { + const additionalExtras = { foo: 'bar' }; + class TestComponent1 { @EventPattern(pattern) public static test() {} @@ -70,6 +72,18 @@ describe('@EventPattern', () => { @EventPattern(pattern, Transport.TCP, extras) public static test() {} } + class TestComponent5 { + @EventPattern(pattern, Transport.TCP, extras) + @(( + (): MethodDecorator => (_target, _key, descriptor) => + Reflect.defineMetadata( + PATTERN_EXTRAS_METADATA, + additionalExtras, + descriptor.value, + ) + )()) + public static test() {} + } it(`should enhance method with ${PATTERN_METADATA} metadata`, () => { const [metadataArg] = Reflect.getMetadata( @@ -86,7 +100,7 @@ describe('@EventPattern', () => { ); expect(metadataArg).to.be.eql(pattern); expect(transportArg).to.be.undefined; - expect(extrasArg).to.be.undefined; + expect(extrasArg).to.be.eql({}); }); it(`should enhance method with ${PATTERN_METADATA}, ${TRANSPORT_METADATA} metadata`, () => { @@ -104,7 +118,7 @@ describe('@EventPattern', () => { ); expect(metadataArg).to.be.eql(pattern); expect(transportArg).to.be.eql(Transport.TCP); - expect(extrasArg).to.be.undefined; + expect(extrasArg).to.be.eql({}); }); it(`should enhance method with ${PATTERN_METADATA}, ${PATTERN_EXTRAS_METADATA} metadata`, () => { @@ -143,5 +157,26 @@ ${PATTERN_EXTRAS_METADATA} metadata`, () => { expect(transportArg).to.be.eql(Transport.TCP); expect(extrasArg).to.be.eql(extras); }); + + it(`should merge with existing ${PATTERN_EXTRAS_METADATA} metadata`, () => { + const [metadataArg] = Reflect.getMetadata( + PATTERN_METADATA, + TestComponent5.test, + ); + const transportArg = Reflect.getMetadata( + TRANSPORT_METADATA, + TestComponent5.test, + ); + const extrasArg = Reflect.getMetadata( + PATTERN_EXTRAS_METADATA, + TestComponent5.test, + ); + expect(metadataArg).to.be.eql(pattern); + expect(transportArg).to.be.eql(Transport.TCP); + expect(extrasArg).to.be.eql({ + ...additionalExtras, + ...extras, + }); + }); }); }); diff --git a/packages/microservices/test/decorators/message-pattern.decorator.spec.ts b/packages/microservices/test/decorators/message-pattern.decorator.spec.ts index d8b62d2f55e..4980c3ff10a 100644 --- a/packages/microservices/test/decorators/message-pattern.decorator.spec.ts +++ b/packages/microservices/test/decorators/message-pattern.decorator.spec.ts @@ -36,6 +36,8 @@ describe('@MessagePattern', () => { }); describe('decorator overloads', () => { + const additionalExtras = { foo: 'bar' }; + class TestComponent1 { @MessagePattern(pattern) public static test() {} @@ -52,6 +54,18 @@ describe('@MessagePattern', () => { @MessagePattern(pattern, Transport.TCP, extras) public static test() {} } + class TestComponent5 { + @MessagePattern(pattern, Transport.TCP, extras) + @(( + (): MethodDecorator => (_target, _key, descriptor) => + Reflect.defineMetadata( + PATTERN_EXTRAS_METADATA, + additionalExtras, + descriptor.value, + ) + )()) + public static test() {} + } it(`should enhance method with ${PATTERN_METADATA} metadata`, () => { const [metadataArg] = Reflect.getMetadata( @@ -68,7 +82,7 @@ describe('@MessagePattern', () => { ); expect(metadataArg).to.be.eql(pattern); expect(transportArg).to.be.undefined; - expect(extrasArg).to.be.undefined; + expect(extrasArg).to.be.eql({}); }); it(`should enhance method with ${PATTERN_METADATA}, ${TRANSPORT_METADATA} metadata`, () => { @@ -86,7 +100,7 @@ describe('@MessagePattern', () => { ); expect(metadataArg).to.be.eql(pattern); expect(transportArg).to.be.eql(Transport.TCP); - expect(extrasArg).to.be.undefined; + expect(extrasArg).to.be.eql({}); }); it(`should enhance method with ${PATTERN_METADATA}, ${PATTERN_EXTRAS_METADATA} metadata`, () => { @@ -125,6 +139,27 @@ ${PATTERN_EXTRAS_METADATA} metadata`, () => { expect(transportArg).to.be.eql(Transport.TCP); expect(extrasArg).to.be.eql(extras); }); + + it(`should merge with existing ${PATTERN_EXTRAS_METADATA} metadata`, () => { + const [metadataArg] = Reflect.getMetadata( + PATTERN_METADATA, + TestComponent5.test, + ); + const transportArg = Reflect.getMetadata( + TRANSPORT_METADATA, + TestComponent5.test, + ); + const extrasArg = Reflect.getMetadata( + PATTERN_EXTRAS_METADATA, + TestComponent5.test, + ); + expect(metadataArg).to.be.eql(pattern); + expect(transportArg).to.be.eql(Transport.TCP); + expect(extrasArg).to.be.eql({ + ...additionalExtras, + ...extras, + }); + }); }); });