diff --git a/services/storage/src/simcore_service_storage/models.py b/services/storage/src/simcore_service_storage/models.py index bc4c9d8b9e6..2e21fc2483c 100644 --- a/services/storage/src/simcore_service_storage/models.py +++ b/services/storage/src/simcore_service_storage/models.py @@ -1,7 +1,7 @@ import datetime import urllib.parse from dataclasses import dataclass -from typing import Literal, NamedTuple, Self +from typing import Any, Literal, NamedTuple from uuid import UUID import arrow @@ -186,14 +186,18 @@ def convert_from_lower_case(cls, v: str) -> str: return f"{v}".upper() return v - @model_validator(mode="after") - def when_directory_force_link_type_and_file_size(self) -> Self: - if self.is_directory is True: + @model_validator(mode="before") + @classmethod + def when_directory_force_link_type_and_file_size(cls, data: Any) -> Any: + assert isinstance(data, dict) + + if bool(data.get("is_directory", False)) is True: # sets directory size by default to undefined - self.file_size = None + if int(data.get("file_size", -1)) < 0: + data["file_size"] = None # only 1 link will be returned manged by the uploader - self.link_type = LinkType.S3 - return self + data["link_type"] = LinkType.S3.value + return data @property def is_v1_upload(self) -> bool: diff --git a/services/storage/tests/unit/test_handlers_files.py b/services/storage/tests/unit/test_handlers_files.py index 88da46df3ad..16e56f7b170 100644 --- a/services/storage/tests/unit/test_handlers_files.py +++ b/services/storage/tests/unit/test_handlers_files.py @@ -1199,8 +1199,8 @@ async def test_copy_as_soft_link( # now let's try with whatever link id file, original_file_uuid = await upload_file( - TypeAdapter(ByteSize, "10Mib") - ).validate_python(faker.file_name()) + TypeAdapter(ByteSize).validate_python("10Mib"), faker.file_name() + ) url = ( client.app.router["copy_as_soft_link"] .url_for( @@ -1208,7 +1208,9 @@ async def test_copy_as_soft_link( ) .with_query(user_id=user_id) ) - link_id = SimcoreS3FileID(f"api/{node_id}/{faker.file_name()}") + link_id = TypeAdapter(SimcoreS3FileID).validate_python( + f"api/{node_id}/{faker.file_name()}" + ) response = await client.post( f"{url}", json=jsonable_encoder(SoftCopyBody(link_id=link_id)) ) @@ -1442,12 +1444,12 @@ async def test_listing_with_project_id_filter( project, src_projects_list = await random_project_with_files( num_nodes=1, file_sizes=(ByteSize(1),), - file_checksums=(SHA256Str(faker.sha256()),), + file_checksums=(TypeAdapter(SHA256Str).validate_python(faker.sha256()),), ) _, _ = await random_project_with_files( num_nodes=1, file_sizes=(ByteSize(1),), - file_checksums=(SHA256Str(faker.sha256()),), + file_checksums=(TypeAdapter(SHA256Str).validate_python(faker.sha256()),), ) assert len(src_projects_list.keys()) > 0 node_id = next(iter(src_projects_list.keys()))