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

Đang mở Phù hợp với người mới
#291 8 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
68/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Ít trao đổi
Công nghệ
python
Lĩnh vực
api

Hướng nghiên cứu

Bắt đầu tại src/cloudevents/core/formats/json.py ở dòng được tham chiếu và kiểm tra cách JSONFormatter xử lý data và Content-Type. Xác minh rằng dữ liệu dạng dictionary tạo ra JSON hợp lệ ngay cả với application/octet-stream, rồi chạy các bài kiểm thử formatter liên quan hoặc bổ sung coverage cho trường hợp này.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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?

Ngôn ngữ chính
Python
Star
342
Fork
65
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của cloudevents/sdk-python

Tất cả issue của cloudevents/sdk-python

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.