Skip to content

bugfix/fix generating api when output has list union types #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 6, 2025
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.0.17

* **Bugfix supporting list union types in response**

## 0.0.16

* **Bugfix for file data deserialization**
Expand Down
21 changes: 12 additions & 9 deletions test/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,9 @@ def generic_validation(self):
]


@pytest.fixture
def file_data() -> FileData:
return FileData(
identifier="mock file data",
connector_type="CON",
source_identifiers=SourceIdentifiers(filename="n", fullpath="n"),
)


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
def test_async_sample_function(file_data):
from test.assets.async_typed_dict_response import async_sample_function as test_fn

Expand All @@ -62,6 +56,9 @@ def test_async_sample_function(file_data):
assert output == {"response": {"a_out": 1, "b_out": 2}}


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
def test_dataclass_response(file_data):
from test.assets.dataclass_response import sample_function_with_path as test_fn

Expand All @@ -85,6 +82,9 @@ def test_dataclass_response(file_data):
}


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
def test_empty_input_and_output(file_data):
from test.assets.empty_input_and_response import SampleClass as TestClass

Expand All @@ -99,6 +99,9 @@ def test_empty_input_and_output(file_data):
assert not output


@pytest.mark.parametrize(
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
)
def test_filedata_meta(file_data):
from test.assets.filedata_meta import Input
from test.assets.filedata_meta import process_input as test_fn
Expand Down
4 changes: 2 additions & 2 deletions test/assets/dataclass_response.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Any, Optional, TypedDict
from typing import Any, Optional, TypedDict, Union

from unstructured_ingest.v2.interfaces import BatchFileData, FileData

Expand All @@ -26,7 +26,7 @@ class SampleFunctionWithPathResponse:


def sample_function_with_path(
file_data: FileData, b: str, c: int, a: Optional[Path] = None
file_data: Union[FileData, BatchFileData], b: str, c: int, a: Optional[Path] = None
) -> SampleFunctionWithPathResponse:
s: list[Any] = [type(a).__name__, f"[exists: {a.exists()}]", a.resolve()] if a else []
s.extend([b, c])
Expand Down
2 changes: 1 addition & 1 deletion unstructured_platform_plugins/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.0.16" # pragma: no cover
__version__ = "0.0.17" # pragma: no cover
19 changes: 14 additions & 5 deletions unstructured_platform_plugins/schema/json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,20 @@ def schema_to_base_model_type(json_type_name, name: str, type_info: dict) -> Typ
t = dict[key_subtype, value_subtype]
if t is list and "items" in type_info and isinstance(type_info["items"], dict):
items = type_info["items"]
item_type_name = items["type"]
subtype = schema_to_base_model_type(
json_type_name=item_type_name, name=f"{name}_type", type_info=items
)
t = list[subtype]
if "anyOf" in items:
subtype_info = [
schema_to_base_model_type(
json_type_name=k["type"], name=f"{k}_{index}_type", type_info=k
)
for index, k in enumerate(items["anyOf"])
]
t = _UnionGenericAlias(Union, tuple(subtype_info))
else:
item_type_name = items["type"]
subtype = schema_to_base_model_type(
json_type_name=item_type_name, name=f"{name}_type", type_info=items
)
t = list[subtype]
if "enum" in type_info and isinstance(type_info["enum"], list):
enum_content = type_info["enum"]
t = Enum(f"{name}_enum", {v: v for v in enum_content})
Expand Down
Loading