Skip to content

fix(qs): keep an explicit empty-string scalar distinct from None - #3845

Open
adhavan18 wants to merge 1 commit into
openai:mainfrom
adhavan18:fix-qs-empty-string-values
Open

fix(qs): keep an explicit empty-string scalar distinct from None#3845
adhavan18 wants to merge 1 commit into
openai:mainfrom
adhavan18:fix-qs-empty-string-values

Conversation

@adhavan18

Copy link
Copy Markdown

Description

Querystring.stringify() drops an explicit empty-string scalar because _stringify_item() checks the serialized value with a truthiness test:

stringify({"filter": ""})  # -> "" (currently)

_primitive_value_to_str serializes both None and "" to the same "" string, so the truthiness check if not serialised: return [] conflates an explicit empty value with omitting the key entirely. In a URL query, filter= is distinct from no filter parameter at all.

Fix: check value is None before serializing, rather than checking the serialized string's truthiness afterward. The array ("comma" format) path was already correct here, since it filters on item is not None, only the scalar path had the bug.

Closes #3837

Verification

Added test_empty_string_scalar_is_kept_distinct_from_none to tests/test_qs.py. Confirmed it fails on unfixed main (stringify({"a": ""}) returns "" instead of "a=") and passes with the fix. Full tests/test_qs.py suite: 12 passed.

_stringify_item serialised both None and "" to the same "" string, then
dropped the param on a truthiness check, so stringify({"a": ""}) produced
"" instead of "a=". An explicit empty value in a query string is not the
same as omitting the key.

Check value is None before serialising, instead of checking the serialised
string's truthiness after the fact. Array format ("comma") already handled
this correctly since it filters on `item is not None`, only the scalar path
had the bug.
@adhavan18
adhavan18 requested a review from a team as a code owner September 10, 2026 14:46
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.

Querystring drops explicit empty-string scalar values

1 participant