Skip to content

Bug: OpenAPI Schema Generation Incorrect for Tuple Type Hint #4130

Open
@Jannchie

Description

@Jannchie

Description

When using a fixed-length tuple type hint like tuple[int, int, int] within a data structure (e.g., msgspec.Struct) returned by a route handler, the generated OpenAPI schema for that field is incorrect. Instead of defining the type for each item position using the array form of items, it generates a schema with "items": {"oneOf": [...]} containing redundant type definitions.

Example:

class Color(Struct):
    rgb: tuple[int, int, int]

Actual Behavior:

The generated OpenAPI schema within components.schemas.Color.properties.rgb is:

{
  "rgb": {
    "items": {
      "oneOf": [
        {
          "type": "integer"
        },
        {
          "type": "integer"
        },
        {
          "type": "integer"
        }
      ]
    },
    "type": "array"
  }
}

It should be:

{
  "rgb": {
    "type": "array",
    "items": [         // Array form to define types per position
      { "type": "integer" }, // Type for index 0
      { "type": "integer" }, // Type for index 1
      { "type": "integer" }  // Type for index 2
    ],
  }
}

URL to code causing the issue

No response

MCVE

from litestar import Litestar, get
from litestar.openapi.config import OpenAPIConfig
from litestar.openapi.plugins import ScalarRenderPlugin
# Assuming Struct is from msgspec, adjust if it's different
from msgspec import Struct

# Define the data structure
class Color(Struct):
    rgb: tuple[int, int, int]

# Define the route handler
@get("/", sync_to_thread=True)
def test() -> Color:
    # Return an instance matching the Color structure
    return Color(rgb=(255, 0, 0))

# Configure the Litestar app with OpenAPI
app = Litestar(
    [test],
    lifespan=[],
    debug=True,
    openapi_config=OpenAPIConfig(
        render_plugins=[ScalarRenderPlugin()],
        title="My API",
        version="1.0.0",
    ),
)

Litestar Version

litestar[standard]>=2.15.2

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