Skip to content

feat(dto-schema-name): add __schema_name__ to dto base #4131

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Jannchie
Copy link
Contributor

Description

I really like this project. I even immediately used it to rewrite my FastAPI-based project.

Especially, it provides DTOs that make it easy to reduce repetitive boilerplate code.

In the current implementation, the DTO generates a Struct for each Response and Request in the backend, which is generally fine. However, this leads to countless repetitive similar definitions in the OpenAPI schema. When I use OpenAPI to generate an SDK, these class names are used for model type definitions, which complicates development due to their differences—unlike with FastAPI, which doesn’t have this issue.

Currently, I find myself unable to specify the __schema_name__ for DTOs, although PydanticDTO has similar functionality.

After reading the source code, it seems not particularly difficult to allow specifying a schema_name. I have added __schema_name__ to the AbstractDTO class and prioritized using this as the model class name.

When generating the schema, it should also allow schemas like classes to be redefined (perhaps it should skip redefinition, but I haven’t figured out how to do that yet.)

In any case, I believe this PR can solve my problem.


I have already tested the effect in my own project, and it looks good. It introduces almost no complexity; you just need to add a single line for __schema_name__.

class TagDTO(MixedDTO[Tag]):
    __schema_name__ = "TagPublic"
    config = DTOConfig(rename_strategy="camel")

before:

image

after:

image

@Jannchie Jannchie requested review from a team as code owners April 20, 2025 04:08
@github-actions github-actions bot added area/dto This PR involves changes to the DTOs area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API size: small type/feat pr/external Triage Required 🏥 This requires triage labels Apr 20, 2025
Copy link

Documentation preview will be available shortly at https://litestar-org.github.io/litestar-docs-preview/4131

@Jannchie Jannchie marked this pull request as draft April 20, 2025 06:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dto This PR involves changes to the DTOs area/openapi This PR involves changes to the OpenAPI schema area/private-api This PR involves changes to the privatized API pr/external size: small Triage Required 🏥 This requires triage type/feat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: Different behavior for generating openapi schema names
1 participant