huma icon indicating copy to clipboard operation
huma copied to clipboard

Parsing a struct in `Multipart Form Data` panics

Open firu11 opened this issue 4 months ago • 0 comments

I've seen that parsing arbitrary types has been implemented just recently.

curl http://localhost:9999/foo \
    --form 'name=josh' \
    --form '[email protected]'

Parses fine into:

type Input struct {
	RawBody huma.MultipartFormFiles[struct {
		Name    string        `form:"name"`
		Content huma.FormFile `form:"content"`
	}]
}

But imagine a scenario where I need to upload a file (possibly binary) to the server. And alongside this file, I want to send some additional metadata as JSON. Like this:

curl http://localhost:9999/foo \
    --form 'metadata={\"client\":\"curl\", \"user_id\":1}' \
    --form '[email protected]'

Then, I would try to define the Input struct as follows:

type Input struct {
	RawBody huma.MultipartFormFiles[struct {
		Metadata Metadata      `form:"metadata" contentType:"application/json"`
		Content  huma.FormFile `form:"content"`
	}]
}
type Metadata struct {
	Client string `json:"client"`
	UserID int    `json:"user_id"`
}

With that, Huma even generates the right OpenAPI spec:

...
Metadata:
  additionalProperties: false
  properties:
    client:
      type: string
    user_id:
      format: int64
      type: integer
  required:
    - client
    - user_id
  type: object
...
requestBody:
  content:
    multipart/form-data:
      encoding:
        content:
          contentType: application/octet-stream
        metadata:
          contentType: text/plain
      schema:
        properties:
          content:
            contentEncoding: binary
            contentMediaType: application/octet-stream
            format: binary
            type: string
          metadata:
            $ref: "#/components/schemas/Metadata"
        type: object
...

But sadly, the parsing then fails when a request is fired.

http: panic serving [::1]:50273: unsupported param type Metadata

firu11 avatar Sep 26 '25 15:09 firu11