Skip to content

Commit cd29b0c

Browse files
Hackshavenclaude
andcommitted
fix(api_search): recurse into nested dicts in _extract_name (#265)
Instead of falling back to str() for nested dict sub-values, recurse via _extract_name to extract a human-readable string from inner keys. Signed-off-by: Eric Hackathorn <erichackathorn@gmail.com> Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent a9d3ee6 commit cd29b0c

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/zyra/connectors/discovery/api_search.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,8 @@ def _extract_name(value: Any) -> str | None:
308308
- ``str`` → returned as-is.
309309
- ``dict`` → drills into common label-like keys (``name``, ``title``,
310310
``id``, ``path``, ``label``, ``url``, ``href``, ``uri``, ``link``)
311-
and returns
312-
the first value that is not ``None`` and not the empty string, as a
313-
string.
311+
and returns the first value that is not ``None`` and not the empty
312+
string, as a string. Nested dicts are handled recursively.
314313
- Other types → converted via ``str()`` as a last resort.
315314
- ``None`` → returns ``None``.
316315
"""
@@ -332,7 +331,11 @@ def _extract_name(value: Any) -> str | None:
332331
):
333332
v = value.get(key)
334333
if v is not None and v != "":
335-
return str(v) if not isinstance(v, str) else v
334+
if isinstance(v, str):
335+
return v
336+
if isinstance(v, dict):
337+
return _extract_name(v)
338+
return str(v)
336339
# No recognizable sub-key; fall through to str()
337340
return str(value)
338341

tests/connectors/test_search_api.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -416,8 +416,9 @@ def test_extract_name_drills_into_dict():
416416
# Non-string values in name-like keys should be stringified
417417
assert _extract_name({"id": 123}) == "123"
418418
assert _extract_name({"name": 42}) == "42"
419-
# Nested dict in a name-like key falls through to str() of sub-value
420-
assert _extract_name({"name": {"id": "x"}}) == "{'id': 'x'}"
419+
# Nested dict in a name-like key is recursively drilled into
420+
assert _extract_name({"name": {"id": "x"}}) == "x"
421+
assert _extract_name({"name": {"title": "inner", "id": "y"}}) == "inner"
421422

422423

423424
def test_normalize_item_nested_dataset_dict():

0 commit comments

Comments
 (0)