|
26 | 26 | | [`--use-field-description-example`](#use-field-description-example) | Add field examples to docstrings. | |
27 | 27 | | [`--use-inline-field-description`](#use-inline-field-description) | Add field descriptions as inline comments. | |
28 | 28 | | [`--use-schema-description`](#use-schema-description) | Use schema description as class docstring. | |
| 29 | +| [`--use-serialization-alias`](#use-serialization-alias) | Use serialization_alias instead of alias for field aliasing ... | |
29 | 30 | | [`--use-title-as-name`](#use-title-as-name) | Use schema title as the generated class name. | |
30 | 31 |
|
31 | 32 | --- |
@@ -3663,6 +3664,86 @@ useful for preserving documentation from your schema in the generated code. |
3663 | 3664 |
|
3664 | 3665 | --- |
3665 | 3666 |
|
| 3667 | +## `--use-serialization-alias` {#use-serialization-alias} |
| 3668 | + |
| 3669 | +Use serialization_alias instead of alias for field aliasing (Pydantic v2 only). |
| 3670 | + |
| 3671 | +The `--use-serialization-alias` flag changes field aliasing to use `serialization_alias` |
| 3672 | +instead of `alias`. This allows setting values using the Pythonic field name while |
| 3673 | +serializing to the original JSON property name. |
| 3674 | + |
| 3675 | +!!! tip "Usage" |
| 3676 | + |
| 3677 | + ```bash |
| 3678 | + datamodel-codegen --input schema.json --use-serialization-alias --output-model-type pydantic_v2.BaseModel # (1)! |
| 3679 | + ``` |
| 3680 | + |
| 3681 | + 1. :material-arrow-left: `--use-serialization-alias` - the option documented here |
| 3682 | + |
| 3683 | +??? example "Examples" |
| 3684 | + |
| 3685 | + **Input Schema:** |
| 3686 | + |
| 3687 | + ```json |
| 3688 | + { |
| 3689 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 3690 | + "title": "Person", |
| 3691 | + "type": "object", |
| 3692 | + "properties": { |
| 3693 | + "first-name": { |
| 3694 | + "type": "string" |
| 3695 | + }, |
| 3696 | + "last-name": { |
| 3697 | + "type": "string" |
| 3698 | + }, |
| 3699 | + "email_address": { |
| 3700 | + "type": "string" |
| 3701 | + } |
| 3702 | + }, |
| 3703 | + "required": ["first-name", "last-name"] |
| 3704 | + } |
| 3705 | + ``` |
| 3706 | + |
| 3707 | + **Output:** |
| 3708 | + |
| 3709 | + === "With Option" |
| 3710 | + |
| 3711 | + ```python |
| 3712 | + # generated by datamodel-codegen: |
| 3713 | + # filename: no_alias.json |
| 3714 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 3715 | + |
| 3716 | + from __future__ import annotations |
| 3717 | + |
| 3718 | + from pydantic import BaseModel, Field |
| 3719 | + |
| 3720 | + |
| 3721 | + class Person(BaseModel): |
| 3722 | + first_name: str = Field(..., serialization_alias='first-name') |
| 3723 | + last_name: str = Field(..., serialization_alias='last-name') |
| 3724 | + email_address: str | None = None |
| 3725 | + ``` |
| 3726 | + |
| 3727 | + === "Without Option" |
| 3728 | + |
| 3729 | + ```python |
| 3730 | + # generated by datamodel-codegen: |
| 3731 | + # filename: no_alias.json |
| 3732 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 3733 | + |
| 3734 | + from __future__ import annotations |
| 3735 | + |
| 3736 | + from pydantic import BaseModel, Field |
| 3737 | + |
| 3738 | + |
| 3739 | + class Person(BaseModel): |
| 3740 | + first_name: str = Field(..., alias='first-name') |
| 3741 | + last_name: str = Field(..., alias='last-name') |
| 3742 | + email_address: str | None = None |
| 3743 | + ``` |
| 3744 | + |
| 3745 | +--- |
| 3746 | + |
3666 | 3747 | ## `--use-title-as-name` {#use-title-as-name} |
3667 | 3748 |
|
3668 | 3749 | Use schema title as the generated class name. |
|
0 commit comments