mateusjunges / laravel-kafka

Use Kafka Producers and Consumers in your laravel app with ease!
https://laravelkafka.com/docs/v2.0/introduction
MIT License
577 stars 81 forks source link

Key cannot be encoded with AVRO when producing #311

Open sash opened 2 months ago

sash commented 2 months ago

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;

mateusjunges commented 2 months ago

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.

sash commented 2 months ago

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.