Skip to content

Bug: OpenAPI Schema Generation Failure for List Containers with Validation Aliases #4288

@NikkosVeraaty

Description

@NikkosVeraaty

Description

When using ResponseSpec with data_container=list[Model], where the Pydantic model contains a field with the validation_alias parameter, an error occurs in generating the OpenAPI schema, leading to a 500 error crash when opening the documentation.

URL to code causing the issue

No response

MCVE

from uuid import uuid4, UUID

import uvicorn
from litestar import Litestar, get, status_codes
from litestar.openapi import OpenAPIConfig, ResponseSpec
from litestar.openapi.plugins import SwaggerRenderPlugin
from pydantic import BaseModel, Field


class Model(BaseModel):
    model_id: UUID | None = Field(validation_alias="model_id_model")
    name: str | None = Field(default=None)


@get(
    path="/",
    responses={
        status_codes.HTTP_200_OK: ResponseSpec(
            data_container=list[Model],
            description="Ok Response",
        ),
    },
)
async def hello_world() -> list[Model]:
    test_obj = {"model_id_model": uuid4(), "name": "test"}
    return [Model.model_validate(test_obj)]


app = Litestar(
    [hello_world],
    openapi_config=OpenAPIConfig(
        title="My API",
        version="1.0.0",
        render_plugins=[SwaggerRenderPlugin()],
        path="/docs",
    ),
)

if __name__ == "__main__":
    uvicorn.run(
        "main:app",
        host="127.0.0.1",
        port=8000,
        reload=True,
    )

Steps to reproduce

  1. Create a Pydantic model with a field using validation_alias
  2. Use this model in ResponseSpec as a list[Model]
  3. Try to open the Swagger documentation (/docs)
  4. Get 500 error instead of displaying documentation

Screenshots

No response

Logs

INFO:     Application startup complete.
INFO:     127.0.0.1:42044 - "GET /docs/swagger HTTP/1.1" 500 Internal Server Error

Litestar Version

2.17.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