Skip to content

Commit 73164df

Browse files
vdusekclaude
andauthored
ci: Use poe tasks for model generation in CI workflow (#705)
## Summary - Add `generate-models-from-file` poe task that accepts a local file path, working around `datamodel-codegen` ignoring `--input` when `url` is set in `pyproject.toml` - Update CI workflow to call poe tasks instead of `datamodel-codegen` directly, ensuring the downloaded artifact from apify-docs PRs is actually used --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cb76ee2 commit 73164df

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

.github/workflows/manual_regenerate_models.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ jobs:
7979
- name: Generate models from OpenAPI spec
8080
run: |
8181
if [[ -f openapi-spec/openapi.json ]]; then
82-
uv run datamodel-codegen --input openapi-spec/openapi.json
82+
uv run poe generate-models-from-file openapi-spec/openapi.json
8383
else
84-
uv run datamodel-codegen
84+
uv run poe generate-models
8585
fi
8686
8787
- name: Commit model changes

pyproject.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,6 @@ context = 7
208208

209209
# https://koxudaxi.github.io/datamodel-code-generator/
210210
[tool.datamodel-codegen]
211-
url = "https://docs.apify.com/api/openapi.json"
212211
input_file_type = "openapi"
213212
output = "src/apify_client/_models.py"
214213
target_python_version = "3.11"
@@ -269,4 +268,8 @@ shell = "./build_api_reference.sh && corepack enable && yarn && uv run yarn star
269268
cwd = "website"
270269

271270
[tool.poe.tasks.generate-models]
272-
shell = "uv run datamodel-codegen"
271+
shell = "uv run datamodel-codegen --url https://docs.apify.com/api/openapi.json && python scripts/postprocess_generated_models.py"
272+
273+
[tool.poe.tasks.generate-models-from-file]
274+
shell = "uv run datamodel-codegen --input $input_file && python scripts/postprocess_generated_models.py"
275+
args = [{ name = "input-file", positional = true, required = true }]
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)