Skip to content

Commit 6065fb0

Browse files
refactor: Update generated models and adapt to them (#704)
This PR updates the auto-generated Pydantic models based on OpenAPI specification changes in [apify-docs PR #2398](apify/apify-docs#2398). ## Summary - Regenerated `_models.py` from updated OpenAPI spec - Adapted resource clients to renamed/restructured models: - `TaskOptions` → `OptionalRunOptions` - `ScheduleCreateActions` → discriminated union in `ScheduleCreate` - `EnvVar` → `EnvVarRequest` for create/update requests --------- Co-authored-by: Vlada Dusek <v.dusek96@gmail.com>
1 parent 73164df commit 6065fb0

File tree

6 files changed

+735
-219
lines changed

6 files changed

+735
-219
lines changed

scripts/postprocess_generated_models.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
- Discriminator field names: datamodel-codegen sometimes emits the JSON property name (camelCase)
55
instead of the Python field name (snake_case) in `Field(discriminator='...')` annotations,
66
particularly when the discriminator is on a schema referenced inside array items.
7+
- Duplicate ErrorType enum: ErrorResponse.yaml inlines the ErrorType enum (due to a Spectral
8+
nested-$ref limitation), causing datamodel-codegen to generate a duplicate `Type(StrEnum)`
9+
class alongside the canonical `ErrorType(StrEnum)`. This script removes the duplicate and
10+
rewires references to use `ErrorType`.
711
"""
812

913
from __future__ import annotations
@@ -31,15 +35,32 @@ def fix_discriminators(content: str) -> str:
3135
return content
3236

3337

38+
def deduplicate_error_type_enum(content: str) -> str:
39+
"""Remove the duplicate `Type` enum and rewire references to `ErrorType`."""
40+
# Remove the entire `class Type(StrEnum): ...` block up to the next class definition.
41+
content = re.sub(
42+
r'\nclass Type\(StrEnum\):.*?(?=\nclass )',
43+
'\n',
44+
content,
45+
flags=re.DOTALL,
46+
)
47+
# Replace standalone `Type` references in type annotations with `ErrorType`.
48+
# Only target annotation contexts (`: Type`, `| Type`, `[Type`).
49+
content = re.sub(r'(?<=: )Type\b|(?<=\| )Type\b|(?<=\[)Type\b', 'ErrorType', content)
50+
# Collapse triple+ blank lines left by the removal.
51+
return re.sub(r'\n{3,}', '\n\n\n', content)
52+
53+
3454
def main() -> None:
3555
content = MODELS_PATH.read_text()
3656
fixed = fix_discriminators(content)
57+
fixed = deduplicate_error_type_enum(fixed)
3758

3859
if fixed != content:
3960
MODELS_PATH.write_text(fixed)
40-
print(f'Fixed discriminator values in {MODELS_PATH}')
61+
print(f'Fixed generated models in {MODELS_PATH}')
4162
else:
42-
print('No discriminator fixes needed')
63+
print('No fixes needed')
4364

4465

4566
if __name__ == '__main__':

0 commit comments

Comments
 (0)