Skip to content

Commit 0345908

Browse files
vdusekclaude
andcommitted
fix: regenerate models with proper class names from OpenAPI spec
Enable `use_title_as_name` in datamodel-codegen config to use schema titles as class names. Add post-processing script to fix discriminator field name bug. Update client code for renamed models (TaskOptions, ScheduleCreateAction, etc.). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent ad53091 commit 0345908

File tree

8 files changed

+635
-204
lines changed

8 files changed

+635
-204
lines changed

.github/workflows/manual_regenerate_models.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ jobs:
8484
uv run datamodel-codegen
8585
fi
8686
87+
- name: Post-process generated models
88+
run: python scripts/fix_generated_models.py
89+
8790
- name: Commit model changes
8891
id: commit
8992
uses: EndBug/add-and-commit@v10

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ wrap_string_literal = true
224224
snake_case_field = true
225225
use_subclass_enum = true
226226
extra_fields = "allow"
227+
use_title_as_name = true
227228
allow_population_by_field_name = true
228229
aliases = "datamodel_codegen_aliases.json"
229230
formatters = ["ruff-check", "ruff-format"]
@@ -269,4 +270,4 @@ shell = "./build_api_reference.sh && corepack enable && yarn && uv run yarn star
269270
cwd = "website"
270271

271272
[tool.poe.tasks.generate-models]
272-
shell = "uv run datamodel-codegen"
273+
shell = "uv run datamodel-codegen && python scripts/fix_generated_models.py"

scripts/fix_generated_models.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""Post-process the generated _models.py to fix known datamodel-codegen issues.
2+
3+
Currently fixes:
4+
- Discriminator field names: datamodel-codegen sometimes emits the JSON property name (camelCase)
5+
instead of the Python field name (snake_case) in `Field(discriminator='...')` annotations,
6+
particularly when the discriminator is on a schema referenced inside array items.
7+
"""
8+
9+
from __future__ import annotations
10+
11+
import re
12+
from pathlib import Path
13+
14+
MODELS_PATH = Path(__file__).resolve().parent.parent / 'src' / 'apify_client' / '_models.py'
15+
16+
# Map of camelCase discriminator values to their snake_case equivalents.
17+
# Add new entries here as needed when the OpenAPI spec introduces new discriminators.
18+
DISCRIMINATOR_FIXES: dict[str, str] = {
19+
'pricingModel': 'pricing_model',
20+
}
21+
22+
23+
def fix_discriminators(content: str) -> str:
24+
"""Replace camelCase discriminator values with their snake_case equivalents."""
25+
for camel, snake in DISCRIMINATOR_FIXES.items():
26+
content = re.sub(
27+
rf"discriminator='{camel}'",
28+
f"discriminator='{snake}'",
29+
content,
30+
)
31+
return content
32+
33+
34+
def main() -> None:
35+
content = MODELS_PATH.read_text()
36+
fixed = fix_discriminators(content)
37+
38+
if fixed != content:
39+
MODELS_PATH.write_text(fixed)
40+
print(f'Fixed discriminator values in {MODELS_PATH}')
41+
else:
42+
print('No discriminator fixes needed')
43+
44+
45+
if __name__ == '__main__':
46+
main()

0 commit comments

Comments
 (0)