cloudevents / cloudevents/sdk-python

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

Offen Anfängerfreundlich
#304 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Vorherrschende Sprache
Python
Sterne
342
Forks
65
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne in cloudevents/v1/kafka/conversion.py und vergleiche to_structured() mit dem neueren, im Issue erwähnten core Kafka binding. Stelle sicher, dass beide Reproduktionen ohne Ausnahmen abgeschlossen werden, content-type weggelassen wird, wenn datacontenttype fehlt, und nicht-stringartige Extension-Attribute in den generierten Headern stringifiziert werden.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
kafka, python
Bereich
distributed-systems
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Ruhig
Klarheit
Klar beschrieben
Anfängerfreundlichkeit
78/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.