Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
atom-andrew opened this issue Sep 27, 2024 · 0 comments
Labels
Bug 🐛 This is something that is not working as expected

Comments

@atom-andrew
Copy link

atom-andrew commented Sep 27, 2024

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

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

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

Screenshots

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)

<!-- POLAR PLEDGE BADGE START -->
---
> [!NOTE]  
> While we are open for sponsoring on [GitHub Sponsors](https://github.com/sponsors/litestar-org/) and 
> [OpenCollective](https://opencollective.com/litestar), we also utilize [Polar.sh](https://polar.sh/) to engage in pledge-based sponsorship.
>
> Check out all issues funded or available for funding [on our Polar.sh dashboard](https://polar.sh/litestar-org)
> * If you would like to see an issue prioritized, make a pledge towards it!
> * We receive the pledge once the issue is completed & verified
> * This, along with engagement in the community, helps us know which features are a priority to our users.

<a href="https://polar.sh/litestar-org/litestar/issues/3760">
<picture>
  <source media="(prefers-color-scheme: dark)" srcset="https://polar.sh/api/github/litestar-org/litestar/issues/3760/pledge.svg?darkmode=1">
  <img alt="Fund with Polar" src="https://polar.sh/api/github/litestar-org/litestar/issues/3760/pledge.svg">
</picture>
</a>
<!-- POLAR PLEDGE BADGE END -->
@atom-andrew atom-andrew added the Bug 🐛 This is something that is not working as expected label Sep 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

1 participant