Kafka `to_binary()` crashes on events without `datacontenttype` or with non-string attributes

オープン 初心者向け
#304 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
78/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
静か
技術スタック
kafka, python

調査の方向性

cloudevents/v1/kafka/conversion.py から始め、issue で言及されている新しい core Kafka binding と to_structured() を比較します。両方の再現が例外なく完了すること、datacontenttype が存在しない場合は content-type を省略すること、生成されたヘッダー内の文字列以外の拡張属性を文字列化することを確認します。

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

説明

Description

to_binary() in cloudevents/v1/kafka/conversion.py raises on two kinds of perfectly valid CloudEvents:

1. Missing datacontenttype (optional attribute) → KeyError

    headers = {}
    if event["datacontenttype"]:                      # KeyError if unset
        headers["content-type"] = event["datacontenttype"].encode("utf-8")

datacontenttype is optional, so event["datacontenttype"] raises KeyError for any event that does not set it. The sibling to_structured() in the same file already guards this with if "datacontenttype" in attrs:.

2. Non-string attribute value → AttributeError

            if value is not None:
                headers["ce_{0}".format(attr)] = value.encode("utf-8")   # AttributeError if not str

CloudEvents extension attributes may be non-string (e.g. Integer/Boolean), so value.encode(...) raises AttributeError: 'int' object has no attribute 'encode'. The newer core Kafka binding already does str(attr_value).encode(...).

Reproduction

from cloudevents.v1.http import CloudEvent
from cloudevents.v1.kafka import to_binary

# 1) no datacontenttype -> KeyError: 'datacontenttype'
to_binary(CloudEvent({"type": "t", "source": "s"}, {"a": 1}))

# 2) non-string extension -> AttributeError: 'int' object has no attribute 'encode'
to_binary(CloudEvent({"type": "t", "source": "s",
                      "datacontenttype": "application/json", "ext1": 5}, {"a": 1}))

Expected behaviour

to_binary() should handle both valid events: omit the content-type header when datacontenttype is absent, and stringify non-string attribute values.

主要言語
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 を短くまとめたダイジェスト。