File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -52,9 +52,22 @@ uv run datamodel-codegen \
5252 --use-annotated \
5353 --field-constraints \
5454 --snake-case-field \
55+ --enum-field-as-literal one \
56+ --use-one-literal-as-default \
5557 --input-file-type openapi \
5658 --formatters ruff-format
5759
60+ # Why --enum-field-as-literal=one + --use-one-literal-as-default?
61+ #
62+ # Without these, single-value enums (used as discriminators on sealed unions
63+ # like AuditMetadata.kind) generate as `kind: Kind` where `Kind(StrEnum)` has
64+ # one entry. Pydantic 2.12+ rejects this when the parent is a discriminated
65+ # union: "Model 'X' needs field 'kind' to be of type `Literal`".
66+ #
67+ # These flags make the codegen emit `kind: Literal["..."] = "..."` instead,
68+ # satisfying the discriminator requirement and making the field optional at
69+ # construction (callers don't need to repeat the discriminator value).
70+
5871# Post-process: inject `model_config = ConfigDict(extra='forbid')` into every
5972# generated class so that requests with unknown fields and responses with
6073# unknown fields BOTH fail loudly. Implements P1 + P2 from
You can’t perform that action at this time.
0 commit comments