Fix Kafka to_binary() crash on missing datacontenttype or non-string attributes - #305
Open
amdadulbari wants to merge 1 commit into
Open
Conversation
The Kafka binary conversion to_binary() read the optional datacontenttype attribute with event["datacontenttype"], raising KeyError for any event that does not set it (datacontenttype is optional in the CloudEvents spec). It also called .encode() directly on attribute values, raising AttributeError for spec-legal non-string extension attributes (e.g. integers). Read datacontenttype via .get() -- matching the sibling to_structured(), which already guards this attribute -- and stringify attribute values before encoding, matching the newer core Kafka binding. Add regression tests for both cases. Signed-off-by: Md. Amdadul Bari Imad <amdadulbari@gmail.com>
amdadulbari
force-pushed
the
fix/kafka-to-binary-optional-datacontenttype
branch
from
August 13, 2026 04:24
d235ef3 to
4e081e9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Related Issue: #304
Type of change:
Description:
to_binary()incloudevents/v1/kafka/conversion.pyraised on two kinds of valid CloudEvents:KeyErrorwhen the optionaldatacontenttypeattribute was absent — it was read viaevent["datacontenttype"]. The siblingto_structured()in the same file already guards this withif "datacontenttype" in attrs:.AttributeErrorwhen an extension attribute had a non-string (e.g.int) value —value.encode("utf-8")was called directly. The newer core Kafka binding already stringifies withstr(attr_value).encode(...).This reads
datacontenttypevia.get()and stringifies attribute values before encoding, soto_binary()handles all valid events.Pre-submission checklist:
git commit --signoff.test_no_datacontenttype,test_non_string_extension_attribute— both fail before this change and pass after).CHANGELOG.md).pre-commit(ruff, ruff-format, mypy) and the Kafka test suite; all checks pass.