#3760·litestar

Bug: `return_dto` not used in openapi spec when `Response` is returned by handler

Author: atom-andrewCreated Sep 27, 2024Updated Sep 29, 2026
LabelsBug :bug:

Description

When I return a Response[MyDTO], I expect the generated openapi spec to include the full dto but it appears to return an empty schema {} instead. https://github.com/litestar-org/litestar/issues/1631 appears to have implemented support for this, but maybe it broke since then. The code below shows that the DTO is being used (because the camelCase rename strategy is applied) , but just not included in the api spec.

URL to code causing the issue

No response

MCVE

python
import json

from litestar.contrib.pydantic import PydanticDTO
from litestar.dto import DTOConfig
from litestar import Controller, get, MediaType, Response, Litestar
from litestar.testing import TestClient
from pydantic import BaseModel


class MyResponseSchema(BaseModel):
    my_name: str


class MyResponseDTO(PydanticDTO[MyResponseSchema]):
    config = DTOConfig(rename_strategy="camel")


class MyController(Controller):
    path = "/my-endpoint"

    @get(
        "",
        media_type=MediaType.JSON,
        return_dto=MyResponseDTO,
    )
    async def get_my_data(self) -> Response[MyResponseSchema]:
        data = MyResponseSchema(id=1, my_name="example")
        return Response(data)


if __name__ == "__main__":
    app = Litestar(
        route_handlers=[MyController],
    )
    print(json.dumps(app.openapi_schema.to_schema(), indent=4))
    with TestClient(app=app) as client:
        print(client.get("/my-endpoint").text)

Steps to reproduce

bash
1. Run the script.
2. Look at the printed openapi spec.

Screenshots

bash
This is the output of that script:


{
    "info": {
        "title": "Litestar API",
        "version": "1.0.0"
    },
    "openapi": "3.1.0",
    "servers": [
        {
            "url": "/"
        }
    ],
    "paths": {
        "/my-endpoint": {
            "get": {
                "summary": "GetMyData",
                "operationId": "MyEndpointGetMyData",
                "responses": {
                    "200": {
                        "description": "Request fulfilled, document follows",
                        "headers": {},
                        "content": {
                            "application/json": {
                                "schema": {}
                            }
                        }
                    }
                },
                "deprecated": false
            }
        }
    },
    "components": {
        "schemas": {}
    }
}
{"myName":"example"}


### Logs

_No response_

### Litestar Version

2.12.1

### Platform

- [ ] Linux
- [X] Mac
- [ ] Windows
- [ ] Other (Please specify in the description above)