`_api_encode` encodes bool/datetime list elements raw (e.g. `flags[0]=True` instead of `true`)
Maintainer thường phản hồi trong vòng 6 ngày
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
- 78/100
Hướng nghiên cứu
Đọc stripe/_encode.py và theo dõi cách _api_encode xử lý các phần tử trực tiếp trong danh sách so với các giá trị cấp cao nhất. Thêm các kiểm thử hồi quy cho phần tử boolean và datetime trong danh sách, sau đó chạy các kiểm thử mã hóa liên quan; công việc hoàn tất khi cả hai được tuần tự hóa thành các giá trị boolean viết thường và dấu thời gian Unix như mong đợi. Tác giả issue cho biết đã chuẩn bị một bản sửa lỗi, nhưng không có pull request nào được liên kết và issue không có bình luận.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
Describe the bug
stripe._encode._api_encode converts bool and datetime values to the forms the API expects at the top level and inside nested dicts (bool → "true"/"false", datetime → unix timestamp), but yields them raw when they are direct elements of a list.
The request body/query string is built with urlencode(list(_api_encode(params))) (in _api_requestor.py), so a list parameter containing a boolean is serialized as "True"/"False" (Python str(bool)) and a list containing a datetime is serialized as its str() repr instead of a unix timestamp. Multipart form encoding is affected too, since MultipartDataGenerator.add_params also goes through _api_encode.
To Reproduce
import datetime
from urllib.parse import urlencode
from stripe._encode import _api_encode
dt = datetime.datetime(2024, 1, 2, 3, 4, 5, tzinfo=datetime.timezone.utc)
print(urlencode(list(_api_encode({"active": True})))) # correct
print(urlencode(list(_api_encode({"flags": [True, False]})))) # wrong
print(urlencode(list(_api_encode({"created": dt})))) # correct
print(urlencode(list(_api_encode({"created": [dt]})))) # wrong
Output:
active=true
flags%5B0%5D=True&flags%5B1%5D=False
created=1704164645
created%5B0%5D=2024-01-02+03%3A04%3A05%2B00%3A00
The top-level bool/datetime are encoded correctly (active=true, created=1704164645); the same values inside a list are not.
Expected behavior
A bool or datetime inside a list should be encoded the same way as at the top level:
flags[0]=true&flags[1]=falsecreated[0]=1704164645
Code snippets
OS
Any (reproduced on Linux)
Language version
Python 3.12 (version-independent)
Library version
main (89e8528)
API version
N/A — client-side request encoding, independent of API version
Additional context
Root cause
In _api_encode (stripe/_encode.py), the list branch handles dict/_data elements and nested list/tuple elements, but every other element — including bool and datetime.datetime — falls through to else: yield (encoded_key, sv) raw. The top-level code has explicit datetime and bool branches; the list branch does not.
(Nested dict values are unaffected because they recurse back through _api_encode; nested lists are unaffected because they go through _encode_nested_dict + recursion. Only direct scalar elements of a list are affected.)
Proposed fix
Mirror the top-level handling in the list branch:
elif isinstance(sv, datetime.datetime):
yield (encoded_key, _encode_datetime(sv))
elif isinstance(sv, bool):
yield (encoded_key, str(sv).lower())
else:
yield (encoded_key, sv)
I've prepared this fix with regression tests on a branch: https://github.com/alphasafal/stripe-python/tree/fix/encode-list-bool-datetime — happy to open a PR if a maintainer would like one (the repo currently limits PR creation to collaborators).
- Ngôn ngữ chính
- Python
- Star
- 2.1k
- Fork
- 547
- Merge trung bình
- 5 ngày 6 giờ
- Pull request đã merge (30 ngày)
- 9
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- 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.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của stripe/stripe-python
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
stripe/stripe-python#1962 ·
Maintainer thường phản hồi trong vòng 6 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 90/100
stripe/stripe-python#1948 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 6 ngày
-
Boolean list elements are encoded as True/False instead of true/falseCó thể làm lại được @inchang-ing đã nhận 13 ngày trước và không có pull request nào đang mở. Đang mởbug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
stripe/stripe-python#1947 · 3 bình luận ·
Maintainer thường phản hồi trong vòng 6 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
stripe/stripe-python#1936 · 2 bình luận ·
Maintainer thường phản hồi trong vòng 6 ngày
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
stripe/stripe-python#1954 · 1 bình luận · 2 reaction ·
Maintainer thường phản hồi trong vòng 6 ngày
Tất cả issue của stripe/stripe-python
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
elviskahoro/sdk-python-attio#144 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 84/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 1 ngày
-
RenyiELBO raises TypeError when model has no sample sitesCó thể đã có người làm @alibb007 đã nhận hôm nay. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 85/100
Maintainer thường phản hồi trong vòng 1 ngày