laravel-kafka
laravel-kafka copied to clipboard
Key cannot be encoded with AVRO when producing
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;
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.
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.