cloudevents / cloudevents/sdk-python
`JSONFormat` shouldn't depend on Content-Type to avoid generating invalid JSON
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 342
- Forks
- 65
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
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?
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza en src/cloudevents/core/formats/json.py en la línea indicada e inspecciona cómo JSONFormatter gestiona data y Content-Type. Verifica que los datos de tipo diccionario produzcan JSON válido incluso con application/octet-stream, y ejecuta las pruebas relevantes del formatter o añade cobertura para este caso.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- api
- Tipo de issue
- Error
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 68/100