Skip to content

fix(jsonschema): guard against empty types list in schema_to_type and create_array_type#1354

Open
devteamaegis wants to merge 1 commit into
PrefectHQ:mainfrom
devteamaegis:fix/indexerror
Open

fix(jsonschema): guard against empty types list in schema_to_type and create_array_type#1354
devteamaegis wants to merge 1 commit into
PrefectHQ:mainfrom
devteamaegis:fix/indexerror

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

Calling jsonschema_to_type({"type": ["null"]}) raises IndexError: list index out of range in schema_to_type(). The code collects Python types for each entry in the list, then filters out type(None). When all entries were "null", the list becomes empty and types[0] crashes. A related crash affects create_array_type(): passing {"type": "array", "items": []} raises TypeError: Cannot take a Union of no types because Union[tuple([])] is invalid.

Why it happens

After the null-filter in schema_to_type(), there is no guard for an empty list before accessing types[0]. In create_array_type(), the tuple passed to Union can be empty when items is [].

Fix

Added if not types: return type(None) before line 302 in schema_to_type(). In create_array_type(), changed Union[tuple(item_types)] to Union[tuple(item_types)] if item_types else Any.

Test

Two new tests in TestEdgeCases: one confirms {"type": ["null"]} returns type(None) without crashing; the other confirms {"type": "array", "items": []} returns a list type that validates an empty list.

Fixes #1353

… 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 PrefectHQ#1353
@github-actions github-actions Bot added the tests label Jun 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: IndexError in schema_to_type() when type list contains only "null"

1 participant