Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions newsfragments/1487.change.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Raise an error, rather than searching nested objects, when a Vuforia
credentials API response has the wanted key at the top level but with
an empty value.
11 changes: 9 additions & 2 deletions src/vws_web_tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1285,8 +1285,15 @@ def _string_from_json(
if _is_json_object(value):
for key in keys:
child = value.get(key)
if isinstance(child, str) and child:
return child
if isinstance(child, str):
if child:
return child
# An object which has the key but with an empty value is
# the object we were looking for, and it is malformed.
# Do not fall through to a nested object which happens
# to have the same key.
message = f"Response included an empty '{key}'."
raise ValueError(message)
for child in value.values():
with contextlib.suppress(ValueError):
return _string_from_json(value=child, keys=keys)
Expand Down
19 changes: 18 additions & 1 deletion tests/test_model_target_web_api_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def test_string_from_json_finds_nested_values() -> None:
"""A non-empty matching string is found recursively."""
result = vws_web_tools._string_from_json(
value={
"client_id": "",
"unrelated": "",
"nested": [
{"clientId": "client-id"},
],
Expand All @@ -102,6 +102,23 @@ def test_string_from_json_finds_nested_values() -> None:
assert result == "client-id"


def test_string_from_json_rejects_an_empty_top_level_value() -> None:
"""An object which has the key with an empty value is not skipped."""
with pytest.raises(
expected_exception=ValueError,
match="Response included an empty 'client_id'",
):
vws_web_tools._string_from_json(
value={
"client_id": "",
"nested": [
{"clientId": "client-id"},
],
},
keys=("client_id", "clientId"),
)


def test_string_from_json_raises_for_missing_values() -> None:
"""A missing matching string raises a useful error."""
with pytest.raises(
Expand Down