|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Patch the OpenAPI spec with temporary overrides before code generation. |
| 3 | +
|
| 4 | +Reads spec_overrides.yaml and applies each override to the OpenAPI spec |
| 5 | +defined in openapitools.json. This allows releasing SDK versions with type |
| 6 | +fixes while waiting for the upstream spec to be updated. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python scripts/patch_openapi_spec.py |
| 10 | +
|
| 11 | +The script is a no-op (exits 0) if spec_overrides.yaml doesn't exist or |
| 12 | +has no overrides defined. |
| 13 | +""" |
| 14 | + |
| 15 | +import json |
| 16 | +import sys |
| 17 | +from pathlib import Path |
| 18 | + |
| 19 | +import yaml |
| 20 | + |
| 21 | +ROOT = Path(__file__).resolve().parent.parent |
| 22 | +OVERRIDES_FILE = ROOT / "spec_overrides.yaml" |
| 23 | +OPENAPITOOLS_FILE = ROOT / "openapitools.json" |
| 24 | + |
| 25 | + |
| 26 | +def load_overrides(): |
| 27 | + if not OVERRIDES_FILE.exists(): |
| 28 | + return [] |
| 29 | + with open(OVERRIDES_FILE) as f: |
| 30 | + data = yaml.safe_load(f) |
| 31 | + if not data or not data.get("overrides"): |
| 32 | + return [] |
| 33 | + return data["overrides"] |
| 34 | + |
| 35 | + |
| 36 | +def get_spec_path(): |
| 37 | + with open(OPENAPITOOLS_FILE) as f: |
| 38 | + config = json.load(f) |
| 39 | + return ROOT / config["inputSpec"] |
| 40 | + |
| 41 | + |
| 42 | +def set_nested(doc, dotted_path, value): |
| 43 | + """Set a value in a nested dict using a dot-separated path, creating intermediate keys as needed.""" |
| 44 | + keys = dotted_path.split(".") |
| 45 | + current = doc |
| 46 | + for key in keys[:-1]: |
| 47 | + if key not in current: |
| 48 | + current[key] = {} |
| 49 | + current = current[key] |
| 50 | + current[keys[-1]] = value |
| 51 | + |
| 52 | + |
| 53 | +def apply_overrides(spec, overrides): |
| 54 | + applied = 0 |
| 55 | + for override in overrides: |
| 56 | + path = override["path"] |
| 57 | + schema = override["schema"] |
| 58 | + set_nested(spec, path, schema) |
| 59 | + print(f" Patched: {path}") |
| 60 | + applied += 1 |
| 61 | + return applied |
| 62 | + |
| 63 | + |
| 64 | +def main(): |
| 65 | + overrides = load_overrides() |
| 66 | + if not overrides: |
| 67 | + print("No spec overrides found, skipping patch step.") |
| 68 | + return |
| 69 | + |
| 70 | + spec_path = get_spec_path() |
| 71 | + if not spec_path.exists(): |
| 72 | + print(f"Error: spec file not found at {spec_path}", file=sys.stderr) |
| 73 | + sys.exit(1) |
| 74 | + |
| 75 | + with open(spec_path) as f: |
| 76 | + spec = yaml.safe_load(f) |
| 77 | + |
| 78 | + print(f"Applying {len(overrides)} override(s) to {spec_path.name}:") |
| 79 | + applied = apply_overrides(spec, overrides) |
| 80 | + |
| 81 | + with open(spec_path, "w") as f: |
| 82 | + yaml.dump(spec, f, default_flow_style=False, sort_keys=False, allow_unicode=True) |
| 83 | + |
| 84 | + print(f"Done. {applied} override(s) applied.") |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + main() |
0 commit comments