cloudevents / cloudevents/sdk-python
`JSONFormat` shouldn't depend on Content-Type to avoid generating invalid JSON
Dieses Issue hat noch niemand übernommen.
- 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
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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