Skip to content

Bug: Cannot explicitely publish message to partition 0 of a Kafka broker #2621

@antoinehumbert

Description

@antoinehumbert

Describe the bug
When publishing a message to a Kafka topic with partition 0 explicitely set, the partition assignment is not taken into account (publishing partition is defined by the partitioner)

How to reproduce
With following code, the message should alwayx be published to partition 0, but this is not the case:

from faststream.kafka import KafkaBroker

broker = KafkaBroker("localhost:9092")

msg = {"foo": "bar"}

await broker.publish(
    msg,
    topic="input_data",
    partition=0,
)

Additional context

The problem is on the following line

partition=partition or self.partition,

Because 0 evaluates to False, the explicit partition assignment is not taken into account and fall backs to the default publisher partition, which is None, finally leading to use of default partitioner.

This line should be replaced with:

partition=partition if partition is not None else self.partition,

I can contribute a PR if you agree

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions