Skip to content
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

Bug: cant upload one file only if type is list[UploadFile] | None #3407

Closed
1 of 4 tasks
euri10 opened this issue Apr 19, 2024 · 4 comments · Fixed by #3408
Closed
1 of 4 tasks

Bug: cant upload one file only if type is list[UploadFile] | None #3407

euri10 opened this issue Apr 19, 2024 · 4 comments · Fixed by #3408
Labels
Bug 🐛 This is something that is not working as expected

Comments

@euri10
Copy link
Contributor

euri10 commented Apr 19, 2024

Description

I though I had the same issue as #2939 but it's slightly different, here is a test case that fails for one file and not for 2 files:

@dataclass
 class OFiles:
    file_list: Optional[List[UploadFile]]


@pytest.mark.parametrize("file_count", (1, 2))
def test_upload_multiple_ofiles_in_model(file_count: int) -> None:
    @post("/")
    async def handler(data: OFiles = Body(media_type=RequestEncodingType.MULTI_PART)) -> None:
        assert len(data.file_list) == file_count

        for file in data.file_list:
            assert await file.read() == b"1"

    with create_test_client([handler]) as client:
        files_to_upload = [("file_list", b"1") for _ in range(file_count)]
        response = client.post("/", files=files_to_upload)

        assert response.status_code == HTTP_201_CREATED

URL to code causing the issue

No response

MCVE

# Your MCVE code here

Steps to reproduce

1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error

Screenshots

"![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

No response

Litestar Version

2.8.2final0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@euri10 euri10 added the Bug 🐛 This is something that is not working as expected label Apr 19, 2024
@JacobCoffee
Copy link
Member

JacobCoffee commented Apr 19, 2024

May need to be a separate issue, but | None also removes the Upload File button in OpenAPI schema docs :(

from litestar import Controller, Litestar, post
from litestar.datastructures import UploadFile
from litestar.enums import RequestEncodingType
from litestar.params import Body


class Files(Controller):
    @post(path="/files/upload")
    async def upload_files(
        self,
        data: UploadFile | None = Body(media_type=RequestEncodingType.MULTI_PART),
    ) -> str:
        return data


app = Litestar(route_handlers=[Files])

Without | None
image

With | None

image

@peterschutt
Copy link
Contributor

May need to be a separate issue, but | None also removes the Upload File button in OpenAPI schema docs :(

from litestar import Controller, Litestar, post
from litestar.datastructures import UploadFile
from litestar.enums import RequestEncodingType
from litestar.params import Body


class Files(Controller):
    @post(path="/files/upload")
    async def upload_files(
        self,
        data: UploadFile | None = Body(media_type=RequestEncodingType.MULTI_PART),
    ) -> str:
        return data


app = Litestar(route_handlers=[Files])

Without | None image

With | None
image

Yeah, I think should be separate. Opened #3411.

Copy link

This issue has been closed in #3408. The change will be included in the upcoming patch release.

Copy link

github-actions bot commented Jun 2, 2024

A fix for this issue has been released in v2.9.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
Status: Closed
Development

Successfully merging a pull request may close this issue.

3 participants