cloudevents / cloudevents/sdk-python

`JSONFormat` shouldn't depend on Content-Type to avoid generating invalid JSON

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

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Hi,
i just upgraded my service to v2.

The problem:

  • the service which produces the cloudevents is maintained by another team, i.e. not in my control
  • they send the events with Content-Type application/octet-stream
  • therefore, in my tests i also emulate sending the events with that Content-Type:
event = CloudEvent(
	attributes={..., "datacontenttype": "application/octet-stream"},
	data=payload,  # <- this is a dictionary
)
message = tobinary_event(event)
client.post("/trigger", content=message.body, headers=message.headers)

now my tests started to send invalid json data (using single quotes instead of double quotes). The reason is this line. E.g. it converts {"asdf": True} to b"{'asdf', True}".

i initially didn't realise this comes from my test case and assumed the SDK generates the invalid data. It took me a while to figure out that only my test setup is wrong.

Proposed solution:

I think str(data).encode("utf-8") is a bad default. Instead, the JSONFormatter should always try to return dumps(data, cls=_JSONEncoderWithDatetime).encode("utf-8"), regardless of the content type. it's the JSONFormatter, so when i pass in a dictionary, i expect it to generate valid json. If it can't, then it's fine to fail with a json.JSONDecodeError or the like. Imo, failing is better than silently generating invalid request data.

Wdyt?

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 src/cloudevents/core/formats/json.py an der referenzierten Zeile und untersuche, wie JSONFormatter data und Content-Type verarbeitet. Überprüfe, dass Dictionary-Daten auch mit application/octet-stream gültiges JSON erzeugen, und führe die relevanten Formatter-Tests aus oder ergänze eine Abdeckung für diesen Fall.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
api
Issue-Typ
Bug
Schwierigkeit
2/5
Geschätzter Aufwand
1-3 Stunden
Aktivitätsstatus
Ruhig
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
68/100

Neue Issues direkt in Ihr Postfach

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