celery / kombu

Messaging library for Python.
http://kombu.readthedocs.org/
BSD 3-Clause "New" or "Revised" License
2.81k stars 919 forks source link

[BUG]datetime type serialization error #1841

Open ponponon opened 5 months ago

ponponon commented 5 months ago

A bug was found in the new version of kombu that serializes datetime type to a dictionary

图片

The cause was that I noticed this strange datetime format in my message queue:{"created_at": {"__type__": "datetime", "__value__": "2024-01-06T16:14:46.368136"}}

Obviously, the format of the serialized message should look like this: {"created at": "2024-01-06T16:27:16.458258"}

But after I reduced the kombu version from 5.3.2 to 5.2.4, it didn't appear again

示例代码:

from kombu import Connection, Exchange, Producer, Queue
from datetime import datetime

# 创建连接
connection = Connection('amqp://pon:pon@192.168.31.245:5672//')

# 创建交换机
exchange = Exchange('ye.events', type='topic')

# 创建队列
queue = Queue('my_queue', exchange=exchange, routing_key='take')

# 创建生产者
producer = Producer(connection, exchange=exchange, routing_key='take')

# 准备要发布的消息
message_body = {
    'created_at': datetime.utcnow()
}

# 发布消息
with producer:
    producer.publish(message_body, declare=[queue])

print("Message published successfully.")
ponponon commented 5 months ago
图片

I found the problem, the problem is with the _as function at https://github.com/celery/kombu/blob/main/kombu/utils/json.py, why introduce this stupid and smartass function?

If you use kombu as the producer and another library (or even another language, such as java) as the consumer, this makes it impossible to deserialise this message body

Can kombu get rid of this boring _as function?


I think that if someone wants to be able to restore the datetime when deserialising, then they should choose pickle instead of changing the datetime of the json into a messy format!