fix(jsonschema): guard against empty types list in schema_to_type and create_array_type#1354
Open
devteamaegis wants to merge 1 commit into
Open
fix(jsonschema): guard against empty types list in schema_to_type and create_array_type#1354devteamaegis wants to merge 1 commit into
devteamaegis wants to merge 1 commit into
Conversation
… 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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's broken
Calling
jsonschema_to_type({"type": ["null"]})raisesIndexError: list index out of rangeinschema_to_type(). The code collects Python types for each entry in the list, then filters outtype(None). When all entries were"null", the list becomes empty andtypes[0]crashes. A related crash affectscreate_array_type(): passing{"type": "array", "items": []}raisesTypeError: Cannot take a Union of no typesbecauseUnion[tuple([])]is invalid.Why it happens
After the null-filter in
schema_to_type(), there is no guard for an empty list before accessingtypes[0]. Increate_array_type(), the tuple passed toUnioncan be empty whenitemsis[].Fix
Added
if not types: return type(None)before line 302 inschema_to_type(). Increate_array_type(), changedUnion[tuple(item_types)]toUnion[tuple(item_types)] if item_types else Any.Test
Two new tests in
TestEdgeCases: one confirms{"type": ["null"]}returnstype(None)without crashing; the other confirms{"type": "array", "items": []}returns a list type that validates an empty list.Fixes #1353