Skip to content

Commit 746b9f9

Browse files
vdusekclaude
andcommitted
ci: move url/input args from datamodel-codegen config to poe tasks
Remove `url` from [tool.datamodel-codegen] config and pass it explicitly via --url/--input in poe tasks. This eliminates the sed workaround in generate-models-from-file and prevents --input from being silently ignored when url is set in config. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f5ed650 commit 746b9f9

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

pyproject.toml

Lines changed: 2 additions & 11 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,16 +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 && python scripts/fix_generated_models.py"
271+
shell = "uv run datamodel-codegen --url https://docs.apify.com/api/openapi.json && python scripts/postprocess_generated_models.py"
273272

274273
[tool.poe.tasks.generate-models-from-file]
275-
# The --input flag is ignored when url is set in pyproject.toml, so we temporarily
276-
# replace url with input, run the generator, and restore the original pyproject.toml.
277-
shell = """
278-
sed -i 's|^url = .*|input = "'"$input_file"'"|' pyproject.toml
279-
uv run datamodel-codegen && python scripts/fix_generated_models.py
280-
STATUS=$?
281-
git checkout pyproject.toml
282-
exit $STATUS
283-
"""
274+
shell = "uv run datamodel-codegen --input $input_file && python scripts/postprocess_generated_models.py"
284275
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)