Skip to content

Bug: Optional[UploadFile] still broken when using msgspec.Struct renaming of files #4890

Description

@brakhane

Description

The vibe coded fix for #4647 doesn't completely fix the problem, it doesn't work for fields that get renamed by msgspec.Struct.

It could also be a deeper issue with litestar not handling renamed fields correctly, haven't checked

URL to code causing the issue

No response

MCVE

import msgspec
from typing import Optional, Annotated
from litestar import Litestar, get, post, Response
from litestar.datastructures import UploadFile
from litestar.params import Body
from litestar.enums import RequestEncodingType


# the rename="camel" is important
class Foo(msgspec.Struct, rename="camel"):
    some_file: Optional[UploadFile] = None

    
@get("/")
async def foo() -> Response:
    return Response("""\
<!DOCTYPE html>
<html>
    <form action="/" method="post" enctype="multipart/form-data">
    <input type="string" name="text"/><br/>
    <input type="file" name="someFile"/><br/>
    <input type="submit"/>
    </form>
</html>
""", media_type="text/html")

@post("/")
async def bar(data: Annotated[Foo, Body(media_type=RequestEncodingType.MULTI_PART)]) -> str:
    return f"ok, file is {data.some_file}"

app = Litestar(route_handlers=[foo, bar])

Steps to reproduce

  1. install litestar[standard] and run the app
  2. do not upload a file, just click submit
  3. Receive error {"status_code":400,"detail":"Validation failed for POST /","extra":[{"message":"Unsupported type: <class 'str'>","key":"someFile","source":"body"}]}

Screenshots

No response

Logs


Litestar Version

2.17.0 doesn't have the bug, all later versions do (tested with 2.24 and 2.22)

Platform

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions