Skip to content

Commit 15cb34a

Browse files
authored
bugfix/Fix support for file data models (#35)
1 parent 57b0cc6 commit 15cb34a

File tree

5 files changed

+30
-14
lines changed

5 files changed

+30
-14
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.16
2+
3+
* **Bugfix for file data deserialization**
4+
15
## 0.0.15
26

37
* **Bugfix for file data serialization**

test/api/test_api.py

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

4040

41-
@pytest.mark.parametrize(
42-
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
43-
)
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+
4450
def test_async_sample_function(file_data):
4551
from test.assets.async_typed_dict_response import async_sample_function as test_fn
4652

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

5864

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

@@ -78,12 +81,10 @@ def test_dataclass_response(file_data):
7881
"resolved": str(current_path.resolve()),
7982
"b": "2",
8083
"c": 1,
84+
"p": not isinstance(file_data, BatchFileData),
8185
}
8286

8387

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

@@ -98,9 +99,6 @@ def test_empty_input_and_output(file_data):
9899
assert not output
99100

100101

101-
@pytest.mark.parametrize(
102-
"file_data", mock_file_data, ids=[type(fd).__name__ for fd in mock_file_data]
103-
)
104102
def test_filedata_meta(file_data):
105103
from test.assets.filedata_meta import Input
106104
from test.assets.filedata_meta import process_input as test_fn

test/assets/dataclass_response.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from pathlib import Path
33
from typing import Any, Optional, TypedDict
44

5+
from unstructured_ingest.v2.interfaces import BatchFileData, FileData
6+
57

68
class SampleFunctionResponse(TypedDict):
79
response: dict[str, Any]
@@ -20,10 +22,11 @@ class SampleFunctionWithPathResponse:
2022
resolved: str
2123
b: str
2224
c: int
25+
p: bool
2326

2427

2528
def sample_function_with_path(
26-
b: str, c: int, a: Optional[Path] = None
29+
file_data: FileData, b: str, c: int, a: Optional[Path] = None
2730
) -> SampleFunctionWithPathResponse:
2831
s: list[Any] = [type(a).__name__, f"[exists: {a.exists()}]", a.resolve()] if a else []
2932
s.extend([b, c])
@@ -33,5 +36,10 @@ def sample_function_with_path(
3336
"resolved": a.resolve(),
3437
"b": b,
3538
"c": c,
39+
"p": (
40+
False
41+
if isinstance(file_data, BatchFileData)
42+
else file_data.source_identifiers.relative_path is not None
43+
),
3644
}
3745
return SampleFunctionWithPathResponse(**resp)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.15" # pragma: no cover
1+
__version__ = "0.0.16" # pragma: no cover

unstructured_platform_plugins/etl_uvicorn/api_generator.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
1212
from pydantic import BaseModel, Field, create_model
1313
from starlette.responses import RedirectResponse
14+
from unstructured_ingest.v2.interfaces.file_data import file_data_from_dict
1415
from uvicorn.config import LOG_LEVELS
1516
from uvicorn.importer import import_from_string
1617

@@ -200,6 +201,11 @@ async def run_job(request: input_schema_model) -> ResponseType:
200201
log_func_and_body(func=func, body=request.json())
201202
# Create dictionary from pydantic model while preserving underlying types
202203
request_dict = {f: getattr(request, f) for f in request.model_fields}
204+
# Make sure nested classes get instantiated correctly
205+
if "file_data" in request_dict:
206+
request_dict["file_data"] = file_data_from_dict(
207+
request_dict["file_data"].model_dump()
208+
)
203209
map_inputs(func=func, raw_inputs=request_dict)
204210
if logger.level == LOG_LEVELS.get("trace", logging.NOTSET):
205211
logger.log(level=logger.level, msg=f"passing inputs to function: {request_dict}")

0 commit comments

Comments
 (0)