Kafka `to_binary()` crashes on events without `datacontenttype` or with non-string attributes
还没有人认领这个 Issue。
评估
- 难度
- 2/5
- 预计耗时
- 1-3 小时
- 新手友好度
- 78/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 冷清
- 技术栈
- kafka, python
调研方向
从 cloudevents/v1/kafka/conversion.py 开始,将 to_structured() 与 issue 中提到的较新的 core Kafka binding 进行比较。验证两种复现都能在不抛出异常的情况下完成,在缺少 datacontenttype 时省略 content-type,并将生成的 headers 中的非字符串扩展属性转换为字符串。
由索引模型根据 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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
cloudevents/sdk-python 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
cloudevents/sdk-python#300 ·
-
enhancement
难度 2/5 1-3 小时 新手友好度 68/100
cloudevents/sdk-python#291 · 8 条评论 ·
-
难度 1/5 1 小时以内 新手友好度 48/100
cloudevents/sdk-python#247 · 2 条评论 ·
-
question
难度 3/5 1-2 天 新手友好度 45/100
cloudevents/sdk-python#246 · 5 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
cloudevents/sdk-python#213 · 3 条评论 · 1 个 reaction ·
查看 cloudevents/sdk-python 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
zostera/django-bootstrap4#894 ·
-
难度 2/5 1-3 小时 新手友好度 78/100
use-agent-os/agent-os#3276 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
难度 2/5 1-3 小时 新手友好度 88/100
NousResearch/hermes-agent#117848 ·
-
难度 2/5 1-3 小时 新手友好度 82/100
zilliztech/memsearch#759 ·