feat(dto-schema-name): add __schema_name__ to dto base #4131
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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
Structfor 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, althoughPydanticDTOhas similar functionality.After reading the source code, it seems not particularly difficult to allow specifying a schema_name. I have added
__schema_name__to theAbstractDTOclass 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__.before:
after: