-
-
Notifications
You must be signed in to change notification settings - Fork 437
Add --class-decorators option for custom model decorators #2760
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
199437e
Add --class-decorators option for custom model decorators
koxudaxi eb39a3b
docs: update CLI reference documentation
github-actions[bot] 5895823
Add test for empty entries in class-decorators
koxudaxi e5bc109
Add parameterized e2e test for class-decorators across all output types
koxudaxi 27a944a
Merge branch 'main' into feature/class-decorators
koxudaxi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| <!-- related-cli-options: --class-decorators, --additional-imports --> | ||
|
|
||
| # Custom Class Decorators | ||
|
|
||
| The `--class-decorators` option adds custom decorators to all generated model classes. This is useful for integrating with serialization libraries like `dataclasses_json`, or adding custom behavior to your models. | ||
|
|
||
| ## Why use this? | ||
|
|
||
| When using `dataclasses.dataclass` output with `--snake-case-field`, Python field names are snake_case but the original JSON keys may be camelCase. Libraries like `dataclasses_json` can handle this conversion automatically via decorators. | ||
|
|
||
| ## Example: Using dataclasses_json | ||
|
|
||
| Convert a JSON Schema with camelCase properties to dataclasses with snake_case fields that serialize back to camelCase. | ||
|
|
||
| **schema.json** | ||
| ```json | ||
| { | ||
| "type": "object", | ||
| "title": "User", | ||
| "properties": { | ||
| "firstName": { "type": "string" }, | ||
| "lastName": { "type": "string" }, | ||
| "emailAddress": { "type": "string" } | ||
| }, | ||
| "required": ["firstName", "lastName"] | ||
| } | ||
| ``` | ||
|
|
||
| ### Without `--class-decorators` | ||
|
|
||
| ```bash | ||
| datamodel-codegen --input schema.json \ | ||
| --output-model-type dataclasses.dataclass \ | ||
| --snake-case-field | ||
| ``` | ||
|
|
||
| **Generated model.py** | ||
| ```python | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
|
|
||
| @dataclass | ||
| class User: | ||
| first_name: str | ||
| last_name: str | ||
| email_address: str | None = None | ||
| ``` | ||
|
|
||
| The field names are snake_case, but there's no way to map them back to the original camelCase JSON keys. | ||
|
|
||
| --- | ||
|
|
||
| ### With `--class-decorators` | ||
|
|
||
| ```bash | ||
| datamodel-codegen --input schema.json \ | ||
| --output-model-type dataclasses.dataclass \ | ||
| --snake-case-field \ | ||
| --class-decorators "@dataclass_json(letter_case=LetterCase.CAMEL)" \ | ||
| --additional-imports "dataclasses_json.dataclass_json,dataclasses_json.LetterCase" | ||
| ``` | ||
|
|
||
| **Generated model.py** | ||
| ```python | ||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
|
|
||
| from dataclasses_json import LetterCase, dataclass_json | ||
|
|
||
|
|
||
| @dataclass_json(letter_case=LetterCase.CAMEL) | ||
| @dataclass | ||
| class User: | ||
| first_name: str | ||
| last_name: str | ||
| email_address: str | None = None | ||
| ``` | ||
|
|
||
| Now serialization automatically converts between snake_case and camelCase: | ||
|
|
||
| ```python | ||
| user = User(first_name="John", last_name="Doe") | ||
| print(user.to_json()) | ||
| # {"firstName": "John", "lastName": "Doe", "emailAddress": null} | ||
| ``` | ||
|
|
||
| ## Usage Notes | ||
|
|
||
| - **Multiple decorators**: Use comma separation for multiple decorators: | ||
| ```bash | ||
| --class-decorators "@decorator1,@decorator2" | ||
| ``` | ||
|
|
||
| - **@ prefix is optional**: Both `@dataclass_json` and `dataclass_json` work - the `@` is added automatically if missing. | ||
|
|
||
| - **Combine with `--additional-imports`**: Always add the required imports for your decorators using `--additional-imports`. | ||
|
|
||
| ## Other Use Cases | ||
|
|
||
| The `--class-decorators` option works with any output model type: | ||
|
|
||
| - **Pydantic models**: Add custom validators or behavior | ||
| - **TypedDict**: Add runtime type checking decorators | ||
| - **msgspec.Struct**: Add custom serialization hooks | ||
|
|
||
| ## See Also | ||
|
|
||
| - [CLI Reference: `--class-decorators`](cli-reference/template-customization.md#class-decorators) - Detailed CLI option documentation | ||
| - [CLI Reference: `--additional-imports`](cli-reference/template-customization.md#additional-imports) - Adding custom imports | ||
|
|
||
| ## Related Issues | ||
|
|
||
| - [#2358](https://github.com/koxudaxi/datamodel-code-generator/issues/2358) - Feature request for dataclasses_json support |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.