Skip to content

Commit cd950cc

Browse files
authored
bugfix/fix generating api when output has list union types (#36)
1 parent 15cb34a commit cd950cc

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.17
2+
3+
* **Bugfix supporting list union types in response**
4+
15
## 0.0.16
26

37
* **Bugfix for file data deserialization**

test/api/test_api.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,9 @@ def generic_validation(self):
3838
]
3939

4040

41-
@pytest.fixture
42-
def file_data() -> FileData:
43-
return FileData(
44-
identifier="mock file data",
45-
connector_type="CON",
46-
source_identifiers=SourceIdentifiers(filename="n", fullpath="n"),
47-
)
48-
49-
41+
@pytest.mark.parametrize(
42+
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
43+
)
5044
def test_async_sample_function(file_data):
5145
from test.assets.async_typed_dict_response import async_sample_function as test_fn
5246

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

6458

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

@@ -85,6 +82,9 @@ def test_dataclass_response(file_data):
8582
}
8683

8784

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

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

101101

102+
@pytest.mark.parametrize(
103+
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
104+
)
102105
def test_filedata_meta(file_data):
103106
from test.assets.filedata_meta import Input
104107
from test.assets.filedata_meta import process_input as test_fn

test/assets/dataclass_response.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from dataclasses import dataclass
22
from pathlib import Path
3-
from typing import Any, Optional, TypedDict
3+
from typing import Any, Optional, TypedDict, Union
44

55
from unstructured_ingest.v2.interfaces import BatchFileData, FileData
66

@@ -26,7 +26,7 @@ class SampleFunctionWithPathResponse:
2626

2727

2828
def sample_function_with_path(
29-
file_data: FileData, b: str, c: int, a: Optional[Path] = None
29+
file_data: Union[FileData, BatchFileData], b: str, c: int, a: Optional[Path] = None
3030
) -> SampleFunctionWithPathResponse:
3131
s: list[Any] = [type(a).__name__, f"[exists: {a.exists()}]", a.resolve()] if a else []
3232
s.extend([b, c])
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.16" # pragma: no cover
1+
__version__ = "0.0.17" # pragma: no cover

unstructured_platform_plugins/schema/json_schema.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,11 +307,20 @@ def schema_to_base_model_type(json_type_name, name: str, type_info: dict) -> Typ
307307
t = dict[key_subtype, value_subtype]
308308
if t is list and "items" in type_info and isinstance(type_info["items"], dict):
309309
items = type_info["items"]
310-
item_type_name = items["type"]
311-
subtype = schema_to_base_model_type(
312-
json_type_name=item_type_name, name=f"{name}_type", type_info=items
313-
)
314-
t = list[subtype]
310+
if "anyOf" in items:
311+
subtype_info = [
312+
schema_to_base_model_type(
313+
json_type_name=k["type"], name=f"{k}_{index}_type", type_info=k
314+
)
315+
for index, k in enumerate(items["anyOf"])
316+
]
317+
t = _UnionGenericAlias(Union, tuple(subtype_info))
318+
else:
319+
item_type_name = items["type"]
320+
subtype = schema_to_base_model_type(
321+
json_type_name=item_type_name, name=f"{name}_type", type_info=items
322+
)
323+
t = list[subtype]
315324
if "enum" in type_info and isinstance(type_info["enum"], list):
316325
enum_content = type_info["enum"]
317326
t = Enum(f"{name}_enum", {v: v for v in enum_content})

0 commit comments

Comments
 (0)