laravel-kafka icon indicating copy to clipboard operation
laravel-kafka copied to clipboard

Key cannot be encoded with AVRO when producing

Open sash opened this issue 1 year ago • 2 comments
trafficstars

Describe the bug The Junges\Kafka\Contracts\ProducerMessage defines public function withKey(?string $key): ProducerMessage; but when using avro we often want to pass more complex data to be encoded in avro. That is currently not possible because the type check fails.

To Reproduce

$producer = \Junges\Kafka\Facades\Kafka::publish('broker')->onTopic('test-topic')->usingSerializer($avroSerializer);

$producer->withMessage(\Junges\Kafka\Message\Message::create('test-topic')->withBody([
    'name' => 'John Doe'
])->withKey(['id' => uuid()]))->send();

fails with $key should be string or null, array given.

Expected behavior Allow assign of complex data to key that will later be convertable to avro.

Additional context Note that the consumer doent have this issue since Junges\Kafka\Contracts\KafkaMessage declares public function getKey(): mixed;

sash avatar Aug 13 '24 11:08 sash

Hey @sash 👋 I believe your argument to withKey should be just the uuid() call. The \RdKafka\ProducerTopic::producev() method accepts only null or string as parameter, that's why the withKey method is type hinted with ?string.

mateusjunges avatar Aug 13 '24 14:08 mateusjunges

Hi @mateusjunges

The key will ultimately be encoded to string by the AVRO encoder, but when I set it I need it to be a complex structure so that avro will work on it to encode it.

sash avatar Aug 15 '24 06:08 sash