diff --git a/packages/microservices/serializers/kafka-request.serializer.ts b/packages/microservices/serializers/kafka-request.serializer.ts index 7c519c4583a..5e2f66c423e 100644 --- a/packages/microservices/serializers/kafka-request.serializer.ts +++ b/packages/microservices/serializers/kafka-request.serializer.ts @@ -21,9 +21,7 @@ export class KafkaRequestSerializer { serialize(value: any) { const isNotKafkaMessage = - isNil(value) || - !isObject(value) || - (!('key' in value) && !('value' in value)); + isNil(value) || !isObject(value) || !('key' in value && 'value' in value); if (isNotKafkaMessage) { value = { value }; diff --git a/packages/microservices/test/serializers/kafka-request.serializer.spec.ts b/packages/microservices/test/serializers/kafka-request.serializer.spec.ts index 566e86d95a8..cc358915c08 100644 --- a/packages/microservices/test/serializers/kafka-request.serializer.spec.ts +++ b/packages/microservices/test/serializers/kafka-request.serializer.spec.ts @@ -97,7 +97,22 @@ describe('KafkaRequestSerializer', () => { }), ).to.deep.eq({ headers: {}, - value: 'string', + value: JSON.stringify({ + value: 'string', + }), + }); + }); + + it('kafka message without value', async () => { + expect( + await instance.serialize({ + key: 'string', + }), + ).to.deep.eq({ + headers: {}, + value: JSON.stringify({ + key: 'string', + }), }); });