Kafka `to_binary()` crashes on events without `datacontenttype` or with non-string attributes
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Quiet
- Tech stack
- kafka, python
- Domain
- distributed-systems
Research direction
Start in cloudevents/v1/kafka/conversion.py and compare to_structured() with the newer core Kafka binding mentioned in the issue. Verify both reproductions complete without exceptions, omit content-type when datacontenttype is absent, and stringify non-string extension attributes in the generated headers.
Written by the indexing model from the issue text.
Description
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.
- Dominant language
- Python
- Stars
- 342
- Forks
- 65
- PR merge metrics
- No merged PRs in 30d
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from cloudevents/sdk-python
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cloudevents/sdk-python#300 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
cloudevents/sdk-python#291 · 8 comments ·
-
Difficulty 1/5 Under an hour Newbie friendliness 48/100
cloudevents/sdk-python#247 · 2 comments ·
-
question
Difficulty 3/5 1-2 days Newbie friendliness 45/100
cloudevents/sdk-python#246 · 5 comments ·
-
Difficulty 3/5 1-2 days Newbie friendliness 35/100
cloudevents/sdk-python#213 · 3 comments · 1 reaction ·
All issues in cloudevents/sdk-python
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
learningequality/ricecooker#747 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
BSData/horus-heresy-3rd-edition#3171 ·
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
run-llama/llama_index#23199 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
KhronosGroup/glTF-Blender-IO#2769 ·