cloudevents / cloudevents/sdk-python

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

Open Beginner friendly
#291 8 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Python
Stars
342
Forks
65
PR merge metrics
No merged PRs in 30d

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in src/cloudevents/core/formats/json.py at the referenced line and inspect JSONFormatter's handling of data and Content-Type. Verify that dictionary data produces valid JSON even with application/octet-stream, and run the relevant formatter tests or add coverage for this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.