|
| 1 | +<!-- related-cli-options: --class-decorators, --additional-imports --> |
| 2 | + |
| 3 | +# Custom Class Decorators |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Why use this? |
| 8 | + |
| 9 | +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. |
| 10 | + |
| 11 | +## Example: Using dataclasses_json |
| 12 | + |
| 13 | +Convert a JSON Schema with camelCase properties to dataclasses with snake_case fields that serialize back to camelCase. |
| 14 | + |
| 15 | +**schema.json** |
| 16 | +```json |
| 17 | +{ |
| 18 | + "type": "object", |
| 19 | + "title": "User", |
| 20 | + "properties": { |
| 21 | + "firstName": { "type": "string" }, |
| 22 | + "lastName": { "type": "string" }, |
| 23 | + "emailAddress": { "type": "string" } |
| 24 | + }, |
| 25 | + "required": ["firstName", "lastName"] |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +### Without `--class-decorators` |
| 30 | + |
| 31 | +```bash |
| 32 | +datamodel-codegen --input schema.json \ |
| 33 | + --output-model-type dataclasses.dataclass \ |
| 34 | + --snake-case-field |
| 35 | +``` |
| 36 | + |
| 37 | +**Generated model.py** |
| 38 | +```python |
| 39 | +from __future__ import annotations |
| 40 | + |
| 41 | +from dataclasses import dataclass |
| 42 | + |
| 43 | + |
| 44 | +@dataclass |
| 45 | +class User: |
| 46 | + first_name: str |
| 47 | + last_name: str |
| 48 | + email_address: str | None = None |
| 49 | +``` |
| 50 | + |
| 51 | +The field names are snake_case, but there's no way to map them back to the original camelCase JSON keys. |
| 52 | + |
| 53 | +--- |
| 54 | + |
| 55 | +### With `--class-decorators` |
| 56 | + |
| 57 | +```bash |
| 58 | +datamodel-codegen --input schema.json \ |
| 59 | + --output-model-type dataclasses.dataclass \ |
| 60 | + --snake-case-field \ |
| 61 | + --class-decorators "@dataclass_json(letter_case=LetterCase.CAMEL)" \ |
| 62 | + --additional-imports "dataclasses_json.dataclass_json,dataclasses_json.LetterCase" |
| 63 | +``` |
| 64 | + |
| 65 | +**Generated model.py** |
| 66 | +```python |
| 67 | +from __future__ import annotations |
| 68 | + |
| 69 | +from dataclasses import dataclass |
| 70 | + |
| 71 | +from dataclasses_json import LetterCase, dataclass_json |
| 72 | + |
| 73 | + |
| 74 | +@dataclass_json(letter_case=LetterCase.CAMEL) |
| 75 | +@dataclass |
| 76 | +class User: |
| 77 | + first_name: str |
| 78 | + last_name: str |
| 79 | + email_address: str | None = None |
| 80 | +``` |
| 81 | + |
| 82 | +Now serialization automatically converts between snake_case and camelCase: |
| 83 | + |
| 84 | +```python |
| 85 | +user = User(first_name="John", last_name="Doe") |
| 86 | +print(user.to_json()) |
| 87 | +# {"firstName": "John", "lastName": "Doe", "emailAddress": null} |
| 88 | +``` |
| 89 | + |
| 90 | +## Usage Notes |
| 91 | + |
| 92 | +- **Multiple decorators**: Use comma separation for multiple decorators: |
| 93 | + ```bash |
| 94 | + --class-decorators "@decorator1,@decorator2" |
| 95 | + ``` |
| 96 | + |
| 97 | +- **@ prefix is optional**: Both `@dataclass_json` and `dataclass_json` work - the `@` is added automatically if missing. |
| 98 | + |
| 99 | +- **Combine with `--additional-imports`**: Always add the required imports for your decorators using `--additional-imports`. |
| 100 | + |
| 101 | +## Other Use Cases |
| 102 | + |
| 103 | +The `--class-decorators` option works with any output model type: |
| 104 | + |
| 105 | +- **Pydantic models**: Add custom validators or behavior |
| 106 | +- **TypedDict**: Add runtime type checking decorators |
| 107 | +- **msgspec.Struct**: Add custom serialization hooks |
| 108 | + |
| 109 | +## See Also |
| 110 | + |
| 111 | +- [CLI Reference: `--class-decorators`](cli-reference/template-customization.md#class-decorators) - Detailed CLI option documentation |
| 112 | +- [CLI Reference: `--additional-imports`](cli-reference/template-customization.md#additional-imports) - Adding custom imports |
| 113 | + |
| 114 | +## Related Issues |
| 115 | + |
| 116 | +- [#2358](https://github.com/koxudaxi/datamodel-code-generator/issues/2358) - Feature request for dataclasses_json support |
0 commit comments