Fix query-string: accept array/bracketed keys and trailing Ɋ
Conversation
- Replace character-whitelist regex with structural pair validation - Accept exactly one trailing '&' (backward-compatible) - Add tests for array-style and bracketed keys (both entry points)
fabiocaccamo
left a comment
There was a problem hiding this comment.
These are the right tests that the implementation must satisfy:
def test_from_query_string_with_array_style_keys(self) -> None:
# array-style keys (PHP / HTML form syntax) are valid query strings
s = "a[]=1&a[]=2"
r = {"a": ["1", "2"]}
d = IODict.from_query_string(s)
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, r)
d = IODict(s, format="query_string")
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, r)
def test_from_query_string_with_bracketed_keys(self) -> None:
s = "user[name]=joe&user[age]=42"
r = {"user": {"name": "joe", "age": "42"}}
d = IODict.from_query_string(s)
self.assertTrue(isinstance(d, dict))
self.assertEqual(d, r)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #586 +/- ##
==========================================
+ Coverage 98.33% 98.38% +0.05%
==========================================
Files 64 64
Lines 2400 2417 +17
==========================================
+ Hits 2360 2378 +18
+ Misses 40 39 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
- Add _parse_bracket_notation to convert parse_qs output:
- a[]=1&a[]=2 -> {"a": ["1", "2"]}
- user[name]=joe&user[age]=42 -> {"user": {"name": "joe", "age": "42"}}
- Update tests to use the correct expected results per owner feedback
Done in 3d5c0ba. Updated the implementation and tests to match your specifications:
The |
Use dict[str, Any] instead of bare dict for return type and result variable
Done in 29c14b3. Pre-commit found two mypy |
from_query_stringrejected valid query strings using array-style (a[]=1&a[]=2) or bracketed (user[name]=joe) keys because the old character-whitelist regex excluded[and]. Trailing&was also silently broken by the new validation.Changes
benedict/serializers/query_string.py: Replace opaque whitelist regex with structural pair validation — split on&, assert each segment matches^[^\s=&]+=[^\s&]*$. Strip exactly one trailing empty segment so"a=1&"remains accepted (backward-compatible).tests/dicts/io/test_io_dict_query_string.py: Addtest_from_query_string_with_array_style_keysandtest_from_query_string_with_bracketed_keys, each asserting both the static method and constructor (IODict(s, format="query_string")) entry points.Checklist before requesting a review