Skip to content

Commit

Permalink
🐛 fix decode: properly account for strict_null_handling when `all…
Browse files Browse the repository at this point in the history
…ow_empty_lists` (#5)
  • Loading branch information
techouse committed Jul 16, 2024
1 parent bf2863f commit 30c29c5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/qs_codec/decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def _parse_object(
root: str = chain[i]

if root == "[]" and options.parse_lists:
if options.allow_empty_lists and leaf == "":
if options.allow_empty_lists and (leaf == "" or (options.strict_null_handling and leaf is None)):
obj = []
else:
obj = list(leaf) if isinstance(leaf, (list, tuple)) else [leaf]
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/decode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ def test_allows_empty_lists_in_obj_values(self) -> None:
assert decode("foo[]&bar=baz", DecodeOptions(allow_empty_lists=True)) == {"foo": [], "bar": "baz"}
assert decode("foo[]&bar=baz", DecodeOptions(allow_empty_lists=False)) == {"foo": [""], "bar": "baz"}

def test_allow_empty_lists_and_strict_null_handling(self) -> None:
assert decode("testEmptyList[]", DecodeOptions(strict_null_handling=True, allow_empty_lists=True)) == {
"testEmptyList": []
}

def test_parses_a_single_nested_string(self) -> None:
assert decode("a[b]=c") == {"a": {"b": "c"}}

Expand Down
6 changes: 6 additions & 0 deletions tests/unit/encode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,12 @@ def test_should_omit_map_key_value_pair_when_value_is_empty_list_and_when_asked(
assert encode({"a": [], "b": "zz"}, options=EncodeOptions(allow_empty_lists=False)) == "b=zz"
assert encode({"a": [], "b": "zz"}, options=EncodeOptions(allow_empty_lists=True)) == "a[]&b=zz"

def test_empty_list_with_strict_null_handling(self) -> None:
assert (
encode({"testEmptyList": []}, options=EncodeOptions(strict_null_handling=True, allow_empty_lists=True))
== "testEmptyList[]"
)

def test_encodes_a_nested_list_value(self) -> None:
assert (
encode(
Expand Down

0 comments on commit 30c29c5

Please sign in to comment.