#Giving field descriptions for DTOs

1 messages · Page 1 of 1 (latest)

snow hull
#

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.

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

fallow mango
#

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])
snow hull
#

my end goal is to use SQLAlchemyDTO

fallow mango
#

I think name: Mapped[Annotated[str, Parameter(description='some random name')]] should work

snow hull
#

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

fallow mango
snow hull
#

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."])
fallow mango
#

this should still be supported

snow hull
#

alright, I'll keep that in mind, thanks

fallow mango
snow hull
#

yeah of course, Field is a pydantic thing

#

but I'd rather not have to duplicate these for the "partial" variant of the DTO

fallow mango
#

wdym? do you mean the partial flag in DTOConfig?

snow hull
#

well, I want to support GET, POST, PATCH and DELETE in my controller

#

and the PATCH endpoint needs to support partial data

#

so yes

fallow mango
#

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
snow hull
#

I don't think I ever saw anything like this in the docs

snow hull
#

yes, in the example there's a separate subclass for the partial updates

fallow mango
#

it depends on how you are creating the DTO

#

this uses the type annotation style

snow hull
#

I'm wondering what comes out of DTOData.as_builtins()

fallow mango
#

its just a dict

snow hull
#

it's typed as Any, not dict, hence my question

fallow mango
#

ohh

snow hull
#

and the docs don't say a word of it being a dict

#

(I need this raw data to create an audit event)

fallow mango
#

fair, just noticed it, <@&1084813023044173866> thoughts?

snow hull
#

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.