Skip to content

Commit

Permalink
Fix qstring.nest returning incorrect value
Browse files Browse the repository at this point in the history
Fix a bug in `qstring.nest` where it returned an incorrect value when
there were more than two query parameters with the same name.

Before:

    >>> qtstring.nest([('foo', '1'), ('foo', '2'), ('foo', '3')])
    {'foo': [['1', '2'], '3']}

After:

    >>> qtstring.nest([('foo', '1'), ('foo', '2'), ('foo', '3')])
    {'foo': ['1', '2', '3']}
  • Loading branch information
jpvanhal committed Sep 27, 2023
1 parent 659ef53 commit 50f8d2f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
12 changes: 12 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ Changelog
- Changed ``qstring.nest`` to use ``dict`` in the returned nested object instead
of ``OrderedDict``. ``dict`` retains insertion order since Python 3.7, so
``OrderedDict`` usage was redundant here.
- Fixed a bug in ``qstring.nest`` where it returned an incorrect value when
there were more than two query parameters with the same name.

Before::

>>> qtstring.nest([('foo', '1'), ('foo', '2'), ('foo', '3')])
{'foo': [['1', '2'], '3']}

After::

>>> qtstring.nest([('foo', '1'), ('foo', '2'), ('foo', '3')])
{'foo': ['1', '2', '3']}

0.2.1 (March 24, 2017)
^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion qstring/nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def _convert_params_list_to_dict(params_list):
params_dict = {}
for key, value in params_list:
if key in params_dict:
params_dict[key] = [params_dict[key], value]
params_dict[key] = (
params_dict[key] + [value]
if isinstance(params_dict[key], list)
else [params_dict[key], value]
)
else:
params_dict[key] = value
return params_dict
Expand Down
2 changes: 1 addition & 1 deletion tests/test_nest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
([('', 'bar')], {'': 'bar'}),
([('foo', 'bar')], {'foo': 'bar'}),
([('foo', ''), ('bar', '')], {'foo': '', 'bar': ''}),
([('foo', '1'), ('foo', '2')], {'foo': ['1', '2']}),
([('foo', '1'), ('foo', '2'), ('foo', '3')], {'foo': ['1', '2', '3']}),
([('foo', '1'), ('bar', '2')], {'foo': '1', 'bar': '2'}),
([('x[y]', '1')], {'x': {'y': '1'}}),
([('x[y][z]', '1')], {'x': {'y': {'z': '1'}}}),
Expand Down

0 comments on commit 50f8d2f

Please sign in to comment.