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

Aperta Adatta ai principianti
#304 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
78/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Tranquilla
Stack tecnologico
kafka, python

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.

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.

Lingua principale
Python
Stelle
342
Fork
65
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di cloudevents/sdk-python

Tutte le issue di cloudevents/sdk-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.