Skip to content

Commit b42b0cd

Browse files
committed
🐛 encode booleans in lowercase to match JavaScript behavior
1 parent 9fddaa6 commit b42b0cd

2 files changed

Lines changed: 24 additions & 3 deletions

File tree

src/qs_codec/encode.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,20 @@ def _encode(
271271

272272
# --- Fast path for primitives/bytes ---------------------------------------------------
273273
if Utils.is_non_nullish_primitive(obj, skip_nulls) or isinstance(obj, bytes):
274+
# When a custom encoder is provided, still coerce Python bools to lowercase JSON style
274275
if callable(encoder):
275276
key_value = prefix if encode_values_only else encoder(prefix, charset, format)
276-
return [f"{formatter(key_value)}={formatter(encoder(obj, charset, format))}"]
277-
return [f"{formatter(prefix)}={formatter(str(obj))}"]
277+
if isinstance(obj, bool):
278+
value_part = "true" if obj else "false"
279+
else:
280+
value_part = encoder(obj, charset, format)
281+
return [f"{formatter(key_value)}={formatter(value_part)}"]
282+
# Default fallback (no custom encoder): ensure lowercase boolean literals
283+
if isinstance(obj, bool):
284+
value_str = "true" if obj else "false"
285+
else:
286+
value_str = str(obj)
287+
return [f"{formatter(prefix)}={formatter(value_str)}"]
278288

279289
values: t.List[t.Any] = []
280290

@@ -288,8 +298,10 @@ def _encode(
288298
# In COMMA mode we join the elements into a single token at this level.
289299
if encode_values_only and callable(encoder):
290300
obj = Utils.apply(obj, encoder)
291-
if obj:
292301
obj_keys_value = ",".join(("" if e is None else str(e)) for e in obj)
302+
else:
303+
obj_keys_value = ",".join(Utils.normalize_comma_elem(e) for e in obj)
304+
if obj:
293305
obj_keys = [{"value": obj_keys_value if obj_keys_value else None}]
294306
else:
295307
obj_keys = [{"value": UNDEFINED}]

src/qs_codec/utils/utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,3 +380,12 @@ def is_non_nullish_primitive(val: t.Any, skip_nulls: bool = False) -> bool:
380380
return True
381381

382382
return False
383+
384+
@staticmethod
385+
def normalize_comma_elem(e: t.Any) -> str:
386+
"""Normalize a value for inclusion in a comma‑joined list."""
387+
if e is None:
388+
return ""
389+
if isinstance(e, bool):
390+
return "true" if e else "false"
391+
return str(e)

0 commit comments

Comments
 (0)