Skip to content

Commit 1623690

Browse files
committed
fix(purl): preserve legacy boolean strings
1 parent 460e1b8 commit 1623690

2 files changed

Lines changed: 33 additions & 3 deletions

File tree

‎socketdev/purl/__init__.py‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
from ..core.dedupe import Dedupe
88

99

10+
def _encode_bool_query_value(value) -> str:
11+
"""Encode typed bools while preserving legacy string query values."""
12+
if isinstance(value, bool):
13+
return "true" if value else "false"
14+
return str(value)
15+
16+
1017
class Purl:
1118
def __init__(self, api):
1219
self.api = api
@@ -46,6 +53,8 @@ def post(
4653
unresolved inputs, so callers can distinguish "no data yet" from "clean".
4754
purl_errors: when ``True`` (``→ purlErrors``), the server includes per-purl
4855
error rows for malformed/unresolvable inputs. ``None`` omits the param.
56+
For backward compatibility, legacy string values passed to the promoted
57+
Boolean parameters are forwarded unchanged.
4958
strict: client-side guard. When ``True``, compares the exact ``purl`` string
5059
of each requested component against the returned ``inputPurl`` (or the
5160
``purl`` fallback). The API defines ``inputPurl`` as the original,
@@ -86,13 +95,13 @@ def post(
8695
# Promote the typed params into query args only when explicitly set, so existing
8796
# callers keep the server's fail-open default (None => omit the param entirely).
8897
if poll is not None:
89-
query_args["poll"] = "true" if poll else "false"
98+
query_args["poll"] = _encode_bool_query_value(poll)
9099
if timeout_sec is not None:
91100
query_args["timeoutSec"] = str(timeout_sec)
92101
if alerts is not None:
93-
query_args["alerts"] = "true" if alerts else "false"
102+
query_args["alerts"] = _encode_bool_query_value(alerts)
94103
if purl_errors is not None:
95-
query_args["purlErrors"] = "true" if purl_errors else "false"
104+
query_args["purlErrors"] = _encode_bool_query_value(purl_errors)
96105
if kwargs:
97106
query_args.update(kwargs)
98107
params = urllib.parse.urlencode(query_args)

‎tests/unit/test_all_endpoints_unit.py‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,27 @@ def test_purl_post_omits_unset_params(self):
524524
self.assertNotIn("alerts=", url)
525525
self.assertNotIn("purlErrors=", url)
526526

527+
def test_purl_post_preserves_legacy_string_boolean_params(self):
528+
"""Promoted params retain the pre-existing stringly kwargs behavior."""
529+
self._mock_purl_ndjson(
530+
'{"inputPurl": "pkg:npm/lodash@4.18.1", '
531+
'"purl": "pkg:npm/lodash@4.18.1", "type": "npm", '
532+
'"name": "lodash", "version": "4.18.1", "valid": true, "alerts": []}'
533+
)
534+
535+
self.sdk.purl.post(
536+
components=[{"purl": "pkg:npm/lodash@4.18.1"}],
537+
org_slug="test-org",
538+
poll="false",
539+
alerts="false",
540+
purl_errors="false",
541+
)
542+
543+
url = self.mock_requests.request.call_args[0][1]
544+
self.assertIn("poll=false", url)
545+
self.assertIn("alerts=false", url)
546+
self.assertIn("purlErrors=false", url)
547+
527548
def test_purl_post_synthetic_pending_scan_row(self):
528549
"""A synthetic pendingScan row (no severity/action) parses without raising KeyError."""
529550
# Synthetic status alerts are built server-side from a minimal {type, key} base.

0 commit comments

Comments
 (0)