|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Flatten ``$ref``-with-siblings so the openapi-generator Rust backend can run. |
| 3 | +
|
| 4 | +OpenAPI 3.1 (JSON Schema 2020-12) lets a ``$ref`` carry sibling keywords — e.g. a |
| 5 | +per-branch ``description`` on a ``oneOf`` member. That is valid, and the Python |
| 6 | +and TypeScript generators consume it fine, but the Rust generator NPEs in |
| 7 | +``AbstractRustCodegen.toModelName`` because it materializes the branch as an |
| 8 | +*unnamed* inline schema. (Reproduced on generator 7.20.0 and 7.22.0; the upstream |
| 9 | +backend has no fix yet.) The fatal case in our spec is ``JobResult``'s ``oneOf``, |
| 10 | +whose four ``$ref`` branches each carry a ``description``. |
| 11 | +
|
| 12 | +We don't need those siblings in the generated Rust client — per-branch ``oneOf`` |
| 13 | +docs don't render in Rust regardless — so for every node that has a ``$ref`` |
| 14 | +alongside other keys we drop the other keys, leaving a pure ref the generator can |
| 15 | +name. This rewrites ONLY the throwaway spec fed to the generator in |
| 16 | +``regenerate.yml``; the canonical spec in www.hotdata.dev (and the Python/TS |
| 17 | +SDKs) are untouched. |
| 18 | +
|
| 19 | +Usage: ``normalize-openapi.py <spec.yaml>`` (edits the file in place). |
| 20 | +""" |
| 21 | + |
| 22 | +from __future__ import annotations |
| 23 | + |
| 24 | +import sys |
| 25 | + |
| 26 | +import yaml |
| 27 | + |
| 28 | + |
| 29 | +def strip_ref_siblings(node: object) -> int: |
| 30 | + """Recursively drop keys that sit alongside a ``$ref``. Returns the count.""" |
| 31 | + removed = 0 |
| 32 | + if isinstance(node, dict): |
| 33 | + if "$ref" in node and len(node) > 1: |
| 34 | + for key in [k for k in node if k != "$ref"]: |
| 35 | + del node[key] |
| 36 | + removed += 1 |
| 37 | + for value in list(node.values()): |
| 38 | + removed += strip_ref_siblings(value) |
| 39 | + elif isinstance(node, list): |
| 40 | + for item in node: |
| 41 | + removed += strip_ref_siblings(item) |
| 42 | + return removed |
| 43 | + |
| 44 | + |
| 45 | +def main() -> int: |
| 46 | + if len(sys.argv) != 2: |
| 47 | + print("usage: normalize-openapi.py <spec.yaml>", file=sys.stderr) |
| 48 | + return 2 |
| 49 | + path = sys.argv[1] |
| 50 | + |
| 51 | + with open(path, encoding="utf-8") as handle: |
| 52 | + spec = yaml.safe_load(handle) |
| 53 | + |
| 54 | + removed = strip_ref_siblings(spec) |
| 55 | + |
| 56 | + with open(path, "w", encoding="utf-8") as handle: |
| 57 | + yaml.safe_dump(spec, handle, sort_keys=False, allow_unicode=True) |
| 58 | + |
| 59 | + print(f"normalize-openapi: stripped {removed} sibling key(s) from $ref nodes in {path}") |
| 60 | + return 0 |
| 61 | + |
| 62 | + |
| 63 | +if __name__ == "__main__": |
| 64 | + raise SystemExit(main()) |
0 commit comments