-
JSON serialization example provided in docs works as expected: @pytest.mark.asyncio()
async def test_json():
broker = fskafka.KafkaBroker()
@broker.subscriber("test-topic")
async def handle(name: str, user_id: int):
assert name == "John"
assert user_id == 1
async with fskafka.TestKafkaBroker(broker) as br:
await br.publish({"name": "John", "user_id": 1}, topic="test-topic") But publishing dict with single key raises async def test_json():
broker = fskafka.KafkaBroker()
@broker.subscriber("test-topic")
async def handle(name: str):
assert name == "John"
async with fskafka.TestKafkaBroker(broker) as br:
await br.publish({"name": "John"}, topic="test-topic")
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
@SirSilver @kumaranvpl it's not a bug, it just an incorrect usage When you are using the single argument as a function annotation, your message body will be serialized as this argument. As an example: async def handler(body: int): ...
# waits any int-serializable message like b"1" In this case your argument name has no matter cuz it is a total message body. Multiple arguments is a shortcut for JSONs. In this case your message will be unpacked and serailized by various fields. Please, reade this part carefully. So async def handler(name: str, id: int): ...
# waits for { "name": "John", "id": 1 } But in your case you are trying to consume string message, but send a Mapping. async def handler(body: str): ...
# waits for any "str", not { "body": "str" } To consume single JSON, you should create a single-field pydantic model and use it for annotation. class User(BaseModel):
name: str
async def handler(body: User): ...
# waits for { "name": "John" } Btw, we can't serialize |
Beta Was this translation helpful? Give feedback.
@SirSilver @kumaranvpl it's not a bug, it just an incorrect usage
When you are using the single argument as a function annotation, your message body will be serialized as this argument.
As an example:
In this case your argument name has no matter cuz it is a total message body.
Multiple arguments is a shortcut for JSONs. In this case your message will be unpacked and serailized by various fields. Please, reade this part carefully.
So
But in your case you are trying to consume string message, but send a Mapping.