`.json()` unable to serialize datetime elements in `data` payload

オープン
#213 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
35/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
python

調査の方向性

Issue では cloudevents.pydantic.CloudEvent.json() がエントリーポイントとして特定されています。まず、datetime の処理を Pydantic BaseModel.json() の処理と比較します。data に datetime を使用するリグレッションケースを追加し、シリアライズによって例外が発生するのではなく文字列が生成されることを確認します。Issue に示されている再現方法で動作を確認します。

索引モデルが issue の本文から書いたものです。

説明

Expected Behavior

The CloudEvent model, like Pydantic's BaseModel, can serialize of datetime properties to strings in the .json() function.

Actual Behavior

The CloudEvent model raises a ValueError from the standard library's json.dumps function when .json() is called on a model containing datetime properties in the data field.

Alternatively, if this is intentional behavior, it would be helpful to have some guidance around what the expected usage is when users want to have dates or timestamps in their CloudEvent data.

Steps to Reproduce the Problem

  1. Init a CloudEvent where some element of the data argument is a datetime instance.
  2. Call .json() on the CloudEvent instance

For example:

from datetime import datetime

from cloudevents.pydantic import CloudEvent

event = CloudEvent(attributes={'source': 'my.source', 'type': 'com.my.type'}, data={'dt': datetime.now()})

# raises TypeError
print(event.json())

By contrast, Pydantic's BaseModel is able to encode datetime properties:

from datetime import datetime

from pydantic import BaseModel

class MyModel(BaseModel):
  dt: datetime

foo = MyModel(dt=datetime.now())

# encodes dt as a string
print(foo.json())

Specifications

  • Platform: MacOS Ventura 13.2.1 (Intel)
  • Python Version: 3.9.9
主要言語
Python
スター
342
フォーク
65
PR マージ指標
30日以内にマージされた PR はありません

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

cloudevents/sdk-python のほかの issue

cloudevents/sdk-python の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。