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

未关闭 适合新手
#291 8 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
68/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
冷清
技术栈
python
领域
api

调研方向

从 src/cloudevents/core/formats/json.py 中引用的行开始,检查 JSONFormatter 对 data 和 Content-Type 的处理。验证字典数据即使在 application/octet-stream 下也能生成有效的 JSON,并运行相关的 formatter 测试或为此情况添加覆盖率。

由索引模型根据 Issue 内容生成。

描述

enhancement

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?

主要语言
Python
星标
342
派生
65
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

cloudevents/sdk-python 的其他 Issue

查看 cloudevents/sdk-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。