`_api_encode` encodes bool/datetime list elements raw (e.g. `flags[0]=True` instead of `true`)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 78/100
調査の方向性
stripe/_encode.py を読み、_api_encode がリストの直接の要素をトップレベルの値と比べてどのように処理するかを追ってください。リスト要素の boolean と datetime に対する回帰テストを追加し、関連するエンコーディングテストを実行してください。両方が期待される小文字の boolean と Unix タイムスタンプにシリアライズされれば完了です。Issue の作成者は修正を準備したと言っていますが、リンクされたプルリクエストはなく、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時間
- マージ済み PR(30日)
- 15
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- 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 が 11 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン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 件 ·
メンテナーはふだん 1 日以内に返信
-
feature-request future
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
stripe/stripe-python#1941 · コメント 9 件 ·
メンテナーはふだん 1 日以内に返信
stripe/stripe-python の issue をすべて見る
似ている issue
-
[Bug]: Responses API 500s on assistant message with empty content list対応中かも @shadowmodder が今日担当しました。 オープンbug
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
vllm-project/vllm#64940 ·
メンテナーはふだん 1 日以内に返信
-
limits
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
Status: Triage Type: Quest
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
GTNewHorizons/GT-New-Horizons-Modpack#27490 ·
メンテナーはふだん 1 日以内に返信
-
LLM.save() discards the model's active prompt style対応中かも @remote-controlled-man が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Lightning-AI/litgpt#2378 ·
メンテナーはふだん 23 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
メンテナーはふだん 1 日以内に返信