cloudevents / cloudevents/sdk-python

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

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

Nessuno ha ancora preso questa issue.

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

Descrizione

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?

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.

Direzione di ricerca

Inizia in src/cloudevents/core/formats/json.py alla riga indicata e analizza la gestione di data e Content-Type da parte di JSONFormatter. Verifica che i dati di tipo dizionario producano JSON valido anche con application/octet-stream, ed esegui i test pertinenti del formatter oppure aggiungi la relativa copertura per questo caso.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
python
Ambito
api
Tipo di issue
Bug
Difficoltà
2/5
Tempo stimato
1-3 ore
Stato di attività
Tranquilla
Chiarezza
Abbastanza chiara
Idoneità per principianti
68/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.