`_api_encode` encodes bool/datetime list elements raw (e.g. `flags[0]=True` instead of `true`)
维护者通常 1 天内回复
还没有人认领这个 Issue。
评估
调研方向
阅读 stripe/_encode.py,并追踪 _api_encode 如何处理列表中的直接元素,以及它如何处理顶层值。为列表中的布尔和 datetime 元素添加回归测试,然后运行相关的编码测试;两者都序列化为预期的小写布尔值和 Unix 时间戳即表示完成。Issue 作者说已准备好修复,但没有关联 pull request,且 issue 没有评论。
由索引模型根据 Issue 内容生成。
描述
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).
- 主要语言
- Python
- 星标
- 2.1k
- 派生
- 547
- 平均合并
- 3 天 21 小时
- 30 天内合并 PR
- 15
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
stripe/stripe-python 的其他 Issue
-
难度 2/5 1-3 小时 新手友好度 90/100
stripe/stripe-python#1948 ·
维护者通常 1 天内回复
-
Boolean list elements are encoded as True/False instead of true/false可能重新可做 @inchang-ing 于 10 天前认领,目前没有进行中的 PR。 未关闭bug
难度 2/5 1-3 小时 新手友好度 78/100
stripe/stripe-python#1947 · 3 条评论 ·
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
stripe/stripe-python#1936 · 2 条评论 ·
维护者通常 1 天内回复
-
难度 3/5 1-2 天 新手友好度 55/100
stripe/stripe-python#1954 · 1 条评论 · 2 个 reaction ·
维护者通常 1 天内回复
-
feature-request future
难度 4/5 3-5 天 新手友好度 55/100
stripe/stripe-python#1941 · 9 条评论 ·
维护者通常 1 天内回复
查看 stripe/stripe-python 的全部 Issue
相似的 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 86/100
NousResearch/hermes-agent#147104 ·
维护者通常 1 天内回复
-
premises-missing
难度 1/5 1 小时以内 新手友好度 88/100
WingedGuardian/GENesis-AGI#3801 ·
维护者通常 1 天内回复
-
[oblt-aw][autodoc] Document new required `shared-org-key` input for `obs-aw-dependency-review`可能已有人在做 @github-actions 今天认领。 未关闭
难度 1/5 1 小时以内 新手友好度 90/100
维护者通常 1 天内回复
-
bot-triaged module: inductor oncall: pt2 topic: fuzzer triaged
难度 2/5 1-3 小时 新手友好度 82/100
维护者通常 1 天内回复
-
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 4 天内回复