Skip to content

Bug: OpenAPI generation with binary request bodies #4482

@burnash

Description

@burnash

Description

When using the body reserved keyword parameter in the route definition, no requestBody is generated in the OpenAPI spec.

Related: https://github.com/orgs/litestar-org/discussions/3038

Root Cause

request_body = None
if data_field := signature_fields.get("data"):
request_body = create_request_body(
self.context, route_handler.handler_id, route_handler.data_dto, data_field
)

The code above only checks data.

Please let me know whether not checking for body is intentional; if it isn't, I'm happy to submit a PR with a fix.

URL to code causing the issue

No response

MCVE

from litestar import Litestar, post
from litestar.response import Response
from litestar.status_codes import HTTP_201_CREATED


@post(
    path="/upload/",
    operation_id="upload_binary",
    status_code=HTTP_201_CREATED,
)
async def upload_binary(
    body: bytes,
    # body: bytes = Body(media_type="application/octet-stream"), # <-- does not work as well
) -> Response[dict[str, str]]:
    return Response(
        content={
            "message": "Data uploaded successfully",
            "size": len(body),
        },
        status_code=HTTP_201_CREATED,
    )


app = Litestar(route_handlers=[upload_binary])

Steps to reproduce

  1. Create the above file and save to app.py
  2. litestar run
  3. curl -O http://localhost:8000/schema/openapi.yaml
  4. cat openapi.yaml
components:
  schemas: {}
info:
  title: Litestar API
  version: 1.0.0
openapi: 3.1.0
paths:
  /upload:
    post:
      deprecated: false
      operationId: upload_binary
      responses:
        '201':
          content:
            application/json:
              schema:
                additionalProperties:
                  type: string
                type: object
          description: Document created, URL follows
          headers: {}
      summary: UploadBinary
servers:
- url: /

I believe the output should look like this:

paths:
  /upload:
    post:
      operationId: upload_binary
      requestBody:  # ← Should be present
        required: true
        content:
          application/octet-stream:
            schema:
              type: string
              format: binary

Naturally, this results in incorrect OpenAPI client generation.

Screenshots

No response

Logs


Litestar Version

2.18.0

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

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions