|
14 | 14 | | [`--field-extra-keys-without-x-prefix`](#field-extra-keys-without-x-prefix) | Include schema extension keys in Field() without requiring '... | |
15 | 15 | | [`--field-include-all-keys`](#field-include-all-keys) | Include all schema keys in Field() json_schema_extra. | |
16 | 16 | | [`--field-type-collision-strategy`](#field-type-collision-strategy) | Rename type class instead of field when names collide (Pydan... | |
| 17 | +| [`--infer-union-variant-names`](#infer-union-variant-names) | Infer names for inline oneOf/anyOf object variants from lite... | |
17 | 18 | | [`--no-alias`](#no-alias) | Disable Field alias generation for non-Python-safe property ... | |
18 | 19 | | [`--original-field-name-delimiter`](#original-field-name-delimiter) | Specify delimiter for original field names when using snake-... | |
19 | 20 | | [`--remove-special-field-name-prefix`](#remove-special-field-name-prefix) | Remove the special prefix from field names. | |
@@ -1742,6 +1743,123 @@ to preserve the original field name, instead of renaming the field and adding an |
1742 | 1743 |
|
1743 | 1744 | --- |
1744 | 1745 |
|
| 1746 | +## `--infer-union-variant-names` {#infer-union-variant-names} |
| 1747 | + |
| 1748 | +Infer names for inline oneOf/anyOf object variants from literal fields. |
| 1749 | + |
| 1750 | +The `--infer-union-variant-names` flag uses branch-local `const` values or |
| 1751 | +single-value enums on a shared discriminator-style property to name generated |
| 1752 | +inline union models. This is useful when inline union branches would otherwise |
| 1753 | +receive position-based names such as `Event` and `Event1`. |
| 1754 | + |
| 1755 | +The literal value can also come from a single-value enum or an internal `$ref`. |
| 1756 | +Existing generated output is preserved unless this option is enabled. |
| 1757 | + |
| 1758 | +**Related:** [`--use-title-as-name`](field-customization.md#use-title-as-name) |
| 1759 | + |
| 1760 | +!!! tip "Usage" |
| 1761 | + |
| 1762 | + ```bash |
| 1763 | + datamodel-codegen --input schema.json --infer-union-variant-names # (1)! |
| 1764 | + ``` |
| 1765 | + |
| 1766 | + 1. :material-arrow-left: `--infer-union-variant-names` - the option documented here |
| 1767 | + |
| 1768 | +??? example "Examples" |
| 1769 | + |
| 1770 | + **Input Schema:** |
| 1771 | + |
| 1772 | + ```json |
| 1773 | + { |
| 1774 | + "$schema": "http://json-schema.org/draft-07/schema#", |
| 1775 | + "title": "WebhookEnvelope", |
| 1776 | + "type": "object", |
| 1777 | + "required": ["event"], |
| 1778 | + "properties": { |
| 1779 | + "event": { |
| 1780 | + "oneOf": [ |
| 1781 | + { |
| 1782 | + "type": "object", |
| 1783 | + "required": ["type", "id"], |
| 1784 | + "properties": { |
| 1785 | + "type": {"const": "message.created"}, |
| 1786 | + "id": {"type": "string"} |
| 1787 | + } |
| 1788 | + }, |
| 1789 | + { |
| 1790 | + "type": "object", |
| 1791 | + "required": ["type", "reason"], |
| 1792 | + "properties": { |
| 1793 | + "type": {"const": "message.failed"}, |
| 1794 | + "reason": {"type": "string"} |
| 1795 | + } |
| 1796 | + } |
| 1797 | + ] |
| 1798 | + } |
| 1799 | + } |
| 1800 | + } |
| 1801 | + ``` |
| 1802 | + |
| 1803 | + **Output:** |
| 1804 | + |
| 1805 | + === "With Option" |
| 1806 | + |
| 1807 | + ```python |
| 1808 | + # generated by datamodel-codegen: |
| 1809 | + # filename: infer_union_variant_names.json |
| 1810 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 1811 | + |
| 1812 | + from __future__ import annotations |
| 1813 | + |
| 1814 | + from typing import Literal |
| 1815 | + |
| 1816 | + from pydantic import BaseModel |
| 1817 | + |
| 1818 | + |
| 1819 | + class EventMessageCreated(BaseModel): |
| 1820 | + type: Literal['message.created'] |
| 1821 | + id: str |
| 1822 | + |
| 1823 | + |
| 1824 | + class EventMessageFailed(BaseModel): |
| 1825 | + type: Literal['message.failed'] |
| 1826 | + reason: str |
| 1827 | + |
| 1828 | + |
| 1829 | + class WebhookEnvelope(BaseModel): |
| 1830 | + event: EventMessageCreated | EventMessageFailed |
| 1831 | + ``` |
| 1832 | + |
| 1833 | + === "Without Option" |
| 1834 | + |
| 1835 | + ```python |
| 1836 | + # generated by datamodel-codegen: |
| 1837 | + # filename: infer_union_variant_names.json |
| 1838 | + # timestamp: 2019-07-26T00:00:00+00:00 |
| 1839 | + |
| 1840 | + from __future__ import annotations |
| 1841 | + |
| 1842 | + from typing import Literal |
| 1843 | + |
| 1844 | + from pydantic import BaseModel |
| 1845 | + |
| 1846 | + |
| 1847 | + class Event(BaseModel): |
| 1848 | + type: Literal['message.created'] |
| 1849 | + id: str |
| 1850 | + |
| 1851 | + |
| 1852 | + class Event1(BaseModel): |
| 1853 | + type: Literal['message.failed'] |
| 1854 | + reason: str |
| 1855 | + |
| 1856 | + |
| 1857 | + class WebhookEnvelope(BaseModel): |
| 1858 | + event: Event | Event1 |
| 1859 | + ``` |
| 1860 | + |
| 1861 | +--- |
| 1862 | + |
1745 | 1863 | ## `--no-alias` {#no-alias} |
1746 | 1864 |
|
1747 | 1865 | Disable Field alias generation for non-Python-safe property names. |
|
0 commit comments