Skip to content

fix: encode empty list/tuple as (key, '') to clear array fields#1817

Open
devteamaegis wants to merge 1 commit into
stripe:masterfrom
devteamaegis:fix/empty-list-encode-802
Open

fix: encode empty list/tuple as (key, '') to clear array fields#1817
devteamaegis wants to merge 1 commit into
stripe:masterfrom
devteamaegis:fix/empty-list-encode-802

Conversation

@devteamaegis
Copy link
Copy Markdown

Problem

Passing an empty list or tuple for an array field in a Stripe API update call silently does nothing — the field is omitted from the request body and the previously-set values remain unchanged.

Reproduction:

stripe.issuing.Cardholder.modify(
    "ich_...",
    spending_controls={"blocked_categories": []},
)
# blocked_categories is NOT cleared — previous values remain.

The documented workaround is to pass "" instead of []:

spending_controls={"blocked_categories": ""},  # works, but not obvious

This is because _api_encode iterates over the list with enumerate, and an empty list yields zero iterations — no key/value pair is emitted and the field disappears from the request.

Fixes #802.

Fix

Before the indexed loop in _api_encode, detect an empty list/tuple and emit (key, "") — the same encoding produced by the "" workaround — so the Stripe API receives an explicit signal to clear the field:

elif isinstance(value, list) or isinstance(value, tuple):
    if not value:
        yield (key, "")   # <-- new: clear the array
    for i, sv in enumerate(value):
        ...

Tests

Two new assertions in tests/test_encode.py:

Test Scenario
test_encode_empty_list_yields_empty_string [][("blocked_categories", "")]
test_encode_empty_tuple_yields_empty_string ()[("tags", "")]

Both pass; existing list-encoding tests are unchanged.

When a list or tuple field is set to [] or () in a Stripe API modify/update
call, _api_encode yielded nothing — the field was omitted from the request
entirely, so the Stripe API left previously-set values unchanged.

The documented workaround was to pass "" (empty string) instead of [],
which encodes to (key, "") and signals to the API to clear the field.

This fix makes an empty list (or tuple) behave identically to the empty-string
workaround: it emits (key, "") before the indexed loop so that a previously-set
array field is cleared rather than silently left as-is.

Fixes stripe#802
@devteamaegis devteamaegis requested a review from a team as a code owner May 28, 2026 16:47
@devteamaegis devteamaegis requested review from xavdid and removed request for a team May 28, 2026 16:47
@cla-assistant
Copy link
Copy Markdown

cla-assistant Bot commented May 28, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Ishaan Samantray seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

1 similar comment
@cla-assistant
Copy link
Copy Markdown

cla-assistant Bot commented May 28, 2026

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


Ishaan Samantray seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Setting a List Field to [] When Modifying a Resource Doesn't Actually Empty The List Field

1 participant