mirror of
https://github.com/usestrix/strix.git
synced 2026-08-28 05:25:00 +00:00
fix(tools): coerce an empty-string list/dict argument to an empty container (#1024)
This commit is contained in:
parent
72833b8e43
commit
c29eb73c7f
2 changed files with 26 additions and 2 deletions
|
|
@ -160,7 +160,9 @@ def _schema_types(spec: dict[str, Any]) -> set[str]:
|
||||||
def _decode_structured(value: str, types: set[str]) -> Any:
|
def _decode_structured(value: str, types: set[str]) -> Any:
|
||||||
stripped = value.strip()
|
stripped = value.strip()
|
||||||
if not stripped:
|
if not stripped:
|
||||||
return value
|
# An empty string is the model's "no value" for a list/dict param; give it
|
||||||
|
# the empty container so it validates instead of failing the type check.
|
||||||
|
return [] if "array" in types else {}
|
||||||
try:
|
try:
|
||||||
decoded = json.loads(stripped)
|
decoded = json.loads(stripped)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
|
|
|
||||||
|
|
@ -70,7 +70,6 @@ async def test_encoded_list_is_decoded_for_an_array_parameter(schema: dict[str,
|
||||||
"auth",
|
"auth",
|
||||||
"Endpoint /admin leaks user data, and session tokens never expire",
|
"Endpoint /admin leaks user data, and session tokens never expire",
|
||||||
'"auth"',
|
'"auth"',
|
||||||
"",
|
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
async def test_free_form_strings_are_never_split_into_an_array(value: str) -> None:
|
async def test_free_form_strings_are_never_split_into_an_array(value: str) -> None:
|
||||||
|
|
@ -79,6 +78,29 @@ async def test_free_form_strings_are_never_split_into_an_array(value: str) -> No
|
||||||
assert parsed["tags"] == value
|
assert parsed["tags"] == value
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
@pytest.mark.parametrize("schema", [_ARRAY, _NULLABLE_ARRAY])
|
||||||
|
@pytest.mark.parametrize("value", ["", " "])
|
||||||
|
async def test_empty_string_becomes_an_empty_array(schema: dict[str, Any], value: str) -> None:
|
||||||
|
parsed = await _roundtrip(schema, {"tags": value})
|
||||||
|
|
||||||
|
assert parsed["tags"] == []
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_empty_string_becomes_an_empty_object() -> None:
|
||||||
|
parsed = await _roundtrip(_OBJECT, {"modifications": ""})
|
||||||
|
|
||||||
|
assert parsed["modifications"] == {}
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_empty_string_for_a_string_parameter_is_untouched() -> None:
|
||||||
|
parsed = await _roundtrip(_STRING, {"todos": ""})
|
||||||
|
|
||||||
|
assert parsed["todos"] == ""
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.asyncio
|
@pytest.mark.asyncio
|
||||||
async def test_encoded_mapping_is_decoded_for_an_object_parameter() -> None:
|
async def test_encoded_mapping_is_decoded_for_an_object_parameter() -> None:
|
||||||
parsed = await _roundtrip(_OBJECT, {"modifications": '{"method": "POST"}'})
|
parsed = await _roundtrip(_OBJECT, {"modifications": '{"method": "POST"}'})
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue