Skip to content

Commit 8302812

Browse files
committed
bug fixes: fixed file data as bytes
1 parent 5530af1 commit 8302812

File tree

2 files changed

+62
-4
lines changed

2 files changed

+62
-4
lines changed

ellar/common/params/resolvers/parameter.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,20 @@
99
sequence_shapes,
1010
sequence_types,
1111
)
12-
from ellar.common.datastructures import UploadFile
1312
from ellar.common.exceptions import RequestValidationError
1413
from ellar.common.interfaces import IExecutionContext
1514
from ellar.common.logger import request_logger
1615
from pydantic.error_wrappers import ErrorWrapper
1716
from pydantic.fields import Undefined
1817
from pydantic.utils import lenient_issubclass
19-
from starlette.datastructures import FormData, Headers, QueryParams
18+
from starlette.datastructures import (
19+
FormData,
20+
Headers,
21+
QueryParams,
22+
)
23+
from starlette.datastructures import (
24+
UploadFile as StarletteUploadFile,
25+
)
2026
from starlette.exceptions import HTTPException
2127

2228
from .base import BaseRouteParameterResolver
@@ -243,7 +249,7 @@ async def process_and_validate(
243249
self, *, values: t.Dict, value: t.Any, loc: t.Tuple
244250
) -> t.Tuple:
245251
if lenient_issubclass(self.model_field.type_, bytes) and isinstance(
246-
value, UploadFile
252+
value, StarletteUploadFile
247253
):
248254
value = await value.read()
249255
elif (

tests/test_routing/test_formparsers.py

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from typing import List, Union
44

55
import pytest
6-
from ellar.common import File, Form, ModuleRouter, UploadFile, serialize_object
6+
from ellar.common import File, Form, ModuleRouter, UploadFile, post, serialize_object
77
from ellar.openapi import OpenAPIDocumentBuilder
88
from ellar.testing import Test
99
from starlette.formparsers import UploadFile as StarletteUploadFile
@@ -373,3 +373,55 @@ def test_multipart_multi_field_app_reads_body(tmpdir):
373373
"/multiple", data={"test1": "key pair"}, files=FORCE_MULTIPART
374374
)
375375
assert response.json() == {"test1": ["key pair"]}
376+
377+
378+
def test_form_file_data_as_bytes(tmpdir):
379+
path = os.path.join(tmpdir, "test.txt")
380+
with open(path, "wb") as file:
381+
file.write(b"<file content>")
382+
383+
@post("/form-bytes")
384+
async def form_file_as_bytes(
385+
ex_file: File[bytes],
386+
):
387+
return {
388+
"file_size": len(ex_file),
389+
}
390+
391+
new_tm = Test.create_test_module()
392+
new_tm.create_application().router.append(form_file_as_bytes)
393+
client = new_tm.get_test_client()
394+
395+
with open(path, "rb") as f:
396+
response = client.post(
397+
"/form-bytes", files={"ex_file": ("test.txt", f, "text/plain")}
398+
)
399+
assert response.json() == {"file_size": 14}
400+
401+
402+
def test_form_file_data_as_list_of_bytes(tmpdir):
403+
path = os.path.join(tmpdir, "test.txt")
404+
with open(path, "wb") as file:
405+
file.write(b"<file content>")
406+
407+
@post("/form-bytes")
408+
async def form_file_as_bytes(
409+
ex_files: File[List[bytes]],
410+
):
411+
return {
412+
"file_size": sum((len(ex_file) for ex_file in ex_files)),
413+
}
414+
415+
new_tm = Test.create_test_module()
416+
new_tm.create_application().router.append(form_file_as_bytes)
417+
client = new_tm.get_test_client()
418+
419+
with open(path, "rb") as f:
420+
response = client.post(
421+
"/form-bytes",
422+
files=[
423+
("ex_files", ("test.txt", f, "test/plain")),
424+
("ex_files", ("test2.txt", f, "text/plain")),
425+
],
426+
)
427+
assert response.json() == {"file_size": 28}

0 commit comments

Comments
 (0)