|
14 | 14 | from __future__ import annotations |
15 | 15 |
|
16 | 16 | import ast |
| 17 | +import json |
17 | 18 | import subprocess |
18 | 19 | import sys |
19 | 20 | import urllib.request |
|
45 | 46 |
|
46 | 47 |
|
47 | 48 | def fetch_spec(source: str) -> Path: |
| 49 | + dest = MODELS_DIR / ".openapi-spec.json" |
48 | 50 | if source.startswith("http://") or source.startswith("https://"): |
49 | | - dest = MODELS_DIR / ".openapi-spec.json" |
50 | 51 | with urllib.request.urlopen(source) as response: |
51 | 52 | dest.write_bytes(response.read()) |
52 | | - return dest |
53 | | - return Path(source) |
| 53 | + else: |
| 54 | + dest.write_bytes(Path(source).read_bytes()) |
| 55 | + return dest |
| 56 | + |
| 57 | + |
| 58 | +def patch_spec(spec_path: Path) -> None: |
| 59 | + """Correct known gaps/bugs in the upstream spec before codegen sees it. |
| 60 | +
|
| 61 | + Both patches below are tracked as issues against Instantly's published |
| 62 | + OpenAPI document; they live here (rather than as post-codegen edits to |
| 63 | + ``_generated.py``) so they survive the next regeneration. |
| 64 | + """ |
| 65 | + spec = json.loads(spec_path.read_text()) |
| 66 | + schemas = spec["components"]["schemas"] |
| 67 | + |
| 68 | + # `GET /accounts?include_tags=true` embeds a `tags` array per account, but |
| 69 | + # the spec only declares that on an anonymous inline extension of the |
| 70 | + # `Account` schema used by that one response, not on `Account` itself. |
| 71 | + # datamodel-code-generator drops the inline extension, so the generated |
| 72 | + # `Account` model (which has `extra="forbid"`) has no `tags` field and |
| 73 | + # raises a `ValidationError` on every `include_tags=True` call. Move the |
| 74 | + # property onto `Account` directly, matching what the API actually sends. |
| 75 | + schemas["Account"]["properties"]["tags"] = { |
| 76 | + "type": ["array", "null"], |
| 77 | + "description": "Tags associated with the account, set to `include_tags` to populate", |
| 78 | + "items": { |
| 79 | + "type": "object", |
| 80 | + "properties": { |
| 81 | + "id": { |
| 82 | + "type": "string", |
| 83 | + "description": "Unique identifier for the custom tag", |
| 84 | + }, |
| 85 | + "label": { |
| 86 | + "type": "string", |
| 87 | + "description": "Display label for the custom tag", |
| 88 | + }, |
| 89 | + "description": { |
| 90 | + "type": ["string", "null"], |
| 91 | + "description": "Detailed description of the custom tag purpose", |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + } |
| 96 | + |
| 97 | + # `CustomTagMapping.resource_type`'s free-text description has the |
| 98 | + # account/campaign mapping backwards relative to its own |
| 99 | + # `x-enumDescriptions` (1: Account, 2: Campaign) and relative to the |
| 100 | + # `toggle-resource` endpoint, which agrees with `x-enumDescriptions`. |
| 101 | + schemas["CustomTagMapping"]["properties"]["resource_type"]["description"] = ( |
| 102 | + "Resource type of custom tag, can be 1 for accounts or 2 for campaigns" |
| 103 | + ) |
| 104 | + |
| 105 | + spec_path.write_text(json.dumps(spec)) |
54 | 106 |
|
55 | 107 |
|
56 | 108 | def run_codegen(spec_path: Path) -> None: |
@@ -99,6 +151,7 @@ def write_init(names: list[str]) -> None: |
99 | 151 | def main() -> None: |
100 | 152 | source = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_SPEC_URL |
101 | 153 | spec_path = fetch_spec(source) |
| 154 | + patch_spec(spec_path) |
102 | 155 | run_codegen(spec_path) |
103 | 156 | names = top_level_names(GENERATED_FILE.read_text()) |
104 | 157 | write_init(names) |
|
0 commit comments