`JSONFormat` shouldn't depend on Content-Type to avoid generating invalid JSON
还没有人认领这个 Issue。
评估
调研方向
从 src/cloudevents/core/formats/json.py 中引用的行开始,检查 JSONFormatter 对 data 和 Content-Type 的处理。验证字典数据即使在 application/octet-stream 下也能生成有效的 JSON,并运行相关的 formatter 测试或为此情况添加覆盖率。
由索引模型根据 Issue 内容生成。
描述
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
cloudevents/sdk-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 78/100
cloudevents/sdk-python#304 ·
-
bug
难度 2/5 1-3 小时 新手友好度 68/100
cloudevents/sdk-python#300 ·
-
难度 1/5 1 小时以内 新手友好度 48/100
cloudevents/sdk-python#247 · 2 条评论 ·
-
question
难度 3/5 1-2 天 新手友好度 45/100
cloudevents/sdk-python#246 · 5 条评论 ·
-
难度 3/5 1-2 天 新手友好度 35/100
cloudevents/sdk-python#213 · 3 条评论 · 1 个 reaction ·
查看 cloudevents/sdk-python 的全部 Issue
相似的 Issue
-
documentation help wanted
难度 2/5 1-3 小时 新手友好度 90/100
-
难度 2/5 1-3 小时 新手友好度 90/100
simonw/sqlite-utils#872 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 82/100
-
难度 2/5 1-3 小时 新手友好度 78/100