Commit 80e854f
committed
fix(codegen): emit single-value Literals for discriminator fields
Pydantic 2.12+ requires a discriminated-union member's discriminator field
to be typed as `Literal[...]`. datamodel-codegen was emitting it as a
single-value `StrEnum` (e.g. `kind: Kind` where `Kind(StrEnum)` had one
entry), which causes Pydantic to refuse to build the union with:
PydanticUserError: Model 'X' needs field 'kind' to be of type `Literal`
This affects any sealed hierarchy on the API side that currently has only
one variant (and trips immediately when an oneOf is exposed before the
second variant lands, e.g. `AuditMetadata` introduced in mono PR #283).
Fix:
- scripts/typegen.sh: pass `--enum-field-as-literal one` and
`--use-one-literal-as-default` to datamodel-codegen.
- Single-value enums now render as `kind: Literal["..."] = "..."`,
which both satisfies the discriminator requirement and lets callers
omit the obvious value.
- Regenerate src/devhelm/_generated.py (net -310/+78 lines: most of
the noisy single-value StrEnum classes collapse into inline Literals).
- Update one negative test (`TestConfirmationPolicyNegative.test_missing_type`)
to reflect the new defaulting behaviour and document the rollback
when a second variant is added.
No public-API changes for callers that already passed the discriminator
explicitly. Branch name matches the mono PR so the spec-evolution harness
on devhelmhq/mono#283 picks it up automatically.
Made-with: Cursor1 parent 7c661d9 commit 80e854f
3 files changed
Lines changed: 103 additions & 315 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
52 | 52 | | |
53 | 53 | | |
54 | 54 | | |
| 55 | + | |
| 56 | + | |
55 | 57 | | |
56 | 58 | | |
57 | 59 | | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
58 | 71 | | |
59 | 72 | | |
60 | 73 | | |
| |||
0 commit comments