Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]datetime type serialization error #1841

Open
ponponon opened this issue Jan 6, 2024 · 1 comment
Open

[BUG]datetime type serialization error #1841

ponponon opened this issue Jan 6, 2024 · 1 comment

Comments

@ponponon
Copy link

ponponon commented Jan 6, 2024

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 ponponon changed the title datetime type serialization error [BUG]datetime type serialization error Jan 6, 2024
@ponponon
Copy link
Author

ponponon commented Jan 7, 2024

图片

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant