From bb2e396fc2bf21e340b4a6863e54d1f6f0b560a4 Mon Sep 17 00:00:00 2001 From: devteamaegis Date: Sun, 31 May 2026 23:28:58 -0400 Subject: [PATCH] fix(jsonschema): guard against empty types list in schema_to_type and create_array_type When the JSON Schema "type" field is a list containing only "null", schema_to_type() filtered out type(None) and crashed with IndexError on the now-empty list. Similarly, create_array_type() crashed with TypeError when "items" was an empty list, because Union[tuple([])] is invalid in Python. Fixes #1353 --- src/marvin/utilities/jsonschema.py | 4 +++- tests/basic/utilities/test_jsonschema.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/marvin/utilities/jsonschema.py b/src/marvin/utilities/jsonschema.py index 2f6c356f8..50ec3d66b 100644 --- a/src/marvin/utilities/jsonschema.py +++ b/src/marvin/utilities/jsonschema.py @@ -217,7 +217,7 @@ def create_array_type( item_types = [schema_to_type(s, schemas) for s in items] from typing import Union - combined = Union[tuple(item_types)] + combined = Union[tuple(item_types)] if item_types else Any base = list[combined] else: # Handle single item schema @@ -296,6 +296,8 @@ def schema_to_type( types.append(schema_to_type(type_schema, schemas)) has_null = type(None) in types types = [t for t in types if t is not type(None)] + if not types: + return type(None) if has_null: from typing import Union diff --git a/tests/basic/utilities/test_jsonschema.py b/tests/basic/utilities/test_jsonschema.py index b55b2dd89..805e1a1e2 100644 --- a/tests/basic/utilities/test_jsonschema.py +++ b/tests/basic/utilities/test_jsonschema.py @@ -1063,6 +1063,17 @@ def test_mixed_type_array(self): result = validator.validate_python(["test", 123, True]) assert result == ["test", 123, True] + def test_null_only_type_list_returns_none_type(self): + # {"type": ["null"]} previously raised IndexError after filtering out NoneType + result = jsonschema_to_type({"type": ["null"]}) + assert result is type(None) + + def test_array_with_empty_items_list_does_not_crash(self): + # {"type": "array", "items": []} previously raised TypeError in Union + result = jsonschema_to_type({"type": "array", "items": []}) + validator = TypeAdapter(result) + assert validator.validate_python([]) == [] + class TestNameHandling: """Test suite for schema name handling."""