fix(qs): keep an explicit empty-string scalar distinct from None - #3845
Open
adhavan18 wants to merge 1 commit into
Open
fix(qs): keep an explicit empty-string scalar distinct from None#3845adhavan18 wants to merge 1 commit into
adhavan18 wants to merge 1 commit into
Conversation
_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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Querystring.stringify() drops an explicit empty-string scalar because _stringify_item() checks the serialized value with a truthiness test:
_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 Nonebefore serializing, rather than checking the serialized string's truthiness afterward. The array ("comma" format) path was already correct here, since it filters onitem 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.