I'm now coming back to Litestar now that I have green light to resume my migration from aiohttp, but a lot of time has passed and Litestar looks very different now. I feel lost as I try to figure out how I'm supposed to add descriptions for individual fields in my DTOs. Am I required to use Pydantic, or is there some way to do this with dataclass based DTOs? I tried scouring the documentation but found nothing.
#Giving field descriptions for DTOs
1 messages · Page 1 of 1 (latest)
Notes for Giving field descriptions for DTOs
At your assistance
@snow hull
No Response?
If no response in a reasonable time, ping @Member.
Closing
To close, type !solve or byte solve.
MCVE
Please include an MCVE so that we can reproduce your issue locally.
Heya, if the end result you want is this with dataclass, you can use the following code
from dataclasses import dataclass
from typing import Annotated
from litestar import Litestar, post
from litestar.dto import DataclassDTO
from litestar.params import Parameter
@dataclass
class Item:
id: Annotated[int, Parameter(description="some random id")]
name: Annotated[str, Parameter(description="some random name")]
@post()
async def create_item(data: Item) -> Item:
return data
@post("dto", dto=DataclassDTO[Item])
async def create_item_with_dto(data: Item) -> Item:
return data
app = Litestar(route_handlers=[create_item, create_item_with_dto])
my end goal is to use SQLAlchemyDTO
I think name: Mapped[Annotated[str, Parameter(description='some random name')]] should work
ok, thanks
is this in the documentation (I'm wondering if I missed it)?
Parameter is documented as "path parameter", didn't seem relevant to DTOs at all
Parameter is documented https://docs.litestar.dev/latest/usage/routing/parameters.html#parameters
but this in particular not really
https://docs.litestar.dev/latest/usage/routing/parameters.html#the-parameter-function
Parameter() is a helper function wrapping a parameter with extra information to be added to the OpenAPI schema.
open for rewording 🙂
when I was last working with Litestar, I had made this according to the docs at the time:
class NewOrganization(BaseModel):
model_config = ConfigDict(title="Organization", extra="forbid")
name: str = Field(description="Displayed name of the organization", examples=["Company Inc."])
this should still be supported
alright, I'll keep that in mind, thanks
this is only for pydantic though
yeah of course, Field is a pydantic thing
but I'd rather not have to duplicate these for the "partial" variant of the DTO
wdym? do you mean the partial flag in DTOConfig?
well, I want to support GET, POST, PATCH and DELETE in my controller
and the PATCH endpoint needs to support partial data
so yes
this?
class Item(UUIDBase):
name: Mapped[Annotated[str, Parameter(description='some random name')]]
@post(dto=SQLAlchemyDTO[Annotated[Item, SQLAlchemyDTOConfig({"id"})]])
async def something(data: Item) -> Item:
return data
@patch(dto=SQLAlchemyDTO[Annotated[Item, SQLAlchemyDTOConfig({"id"}, partial=True)]])
async def something_partial(data: DTOData[Item]) -> Item:
return data
I don't think I ever saw anything like this in the docs
yes, in the example there's a separate subclass for the partial updates
I'm wondering what comes out of DTOData.as_builtins()
its just a dict
it's typed as Any, not dict, hence my question
ohh
and the docs don't say a word of it being a dict
(I need this raw data to create an audit event)
fair, just noticed it, <@&1084813023044173866> thoughts?
I have to say that a lot of things have actually improved since my last foray to the world of Litestar. I don't think controller-level DTOs were a thing, and I don't remember seeing this useful DTOData class back then.