Skip to content

Commit ff0e4c6

Browse files
authored
Merge pull request #10 from devhelmhq/chore/end-1098-chunk1-typed-internals
fix(codegen): emit single-value Literals for discriminator fields
2 parents 7c661d9 + 80e854f commit ff0e4c6

3 files changed

Lines changed: 103 additions & 315 deletions

File tree

scripts/typegen.sh

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)