cloudevents / cloudevents/sdk-python
Kafka `to_binary()` crashes on events without `datacontenttype` or with non-string attributes
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 342
- Fork
- 65
- Metriche di merge delle PR
- Nessuna PR unita negli ultimi 30g
Descrizione
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.
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia in cloudevents/v1/kafka/conversion.py e confronta to_structured() con il binding Kafka core più recente menzionato nell’issue. Verifica che entrambe le riproduzioni vengano completate senza eccezioni, che content-type venga omesso quando datacontenttype è assente e che gli attributi di estensione non stringa vengano convertiti in stringhe negli header generati.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- kafka, python
- Ambito
- distributed-systems
- Tipo di issue
- Bug
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Stato di attività
- Tranquilla
- Chiarezza
- Specificata chiaramente
- Idoneità per principianti
- 78/100