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. #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.
No response
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)
1. Run the script.
2. Look at the printed openapi spec.
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)
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too