|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Patch the swagger2openapi-converted Docker spec for jane-php/open-api-3-1. |
| 3 | +
|
| 4 | +Idempotent. Run after swagger2openapi conversion, before composer generate. |
| 5 | +
|
| 6 | +Reads the spec path from .jane-openapi if invoked from the repo root, else |
| 7 | +defaults to spec/docker-v1.54-patched.yaml. |
| 8 | +
|
| 9 | +Requires: ruamel.yaml (pip install ruamel.yaml) |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import re |
| 14 | +import sys |
| 15 | +from pathlib import Path |
| 16 | + |
| 17 | +from ruamel.yaml import YAML |
| 18 | +from ruamel.yaml.comments import CommentedSeq |
| 19 | + |
| 20 | + |
| 21 | +def find_spec_path() -> Path: |
| 22 | + repo_root = Path(__file__).resolve().parent.parent |
| 23 | + jane_cfg = repo_root / ".jane-openapi" |
| 24 | + if jane_cfg.exists(): |
| 25 | + m = re.search(r"openapi-file'\s*=>.*?'(spec/[^']+)'", jane_cfg.read_text()) |
| 26 | + if m: |
| 27 | + return repo_root / m.group(1) |
| 28 | + m = re.search(r"openapi-file'\s*=>.*?/spec/([^']+)'", jane_cfg.read_text()) |
| 29 | + if m: |
| 30 | + return repo_root / "spec" / m.group(1) |
| 31 | + return repo_root / "spec" / "docker-v1.54-patched.yaml" |
| 32 | + |
| 33 | + |
| 34 | +def make_nullable(node) -> bool: |
| 35 | + if not isinstance(node, dict): |
| 36 | + return False |
| 37 | + if "nullable" in node: |
| 38 | + if node["nullable"] is True: |
| 39 | + del node["nullable"] |
| 40 | + else: |
| 41 | + return False |
| 42 | + if "$ref" in node: |
| 43 | + return False |
| 44 | + t = node.get("type") |
| 45 | + if t is None: |
| 46 | + return False |
| 47 | + if isinstance(t, str): |
| 48 | + if t == "null": |
| 49 | + return False |
| 50 | + seq = CommentedSeq([t, "null"]) |
| 51 | + seq.fa.set_flow_style() |
| 52 | + node["type"] = seq |
| 53 | + return True |
| 54 | + if isinstance(t, list): |
| 55 | + if "null" not in t: |
| 56 | + t.append("null") |
| 57 | + return True |
| 58 | + return False |
| 59 | + |
| 60 | + |
| 61 | +def is_object_with_ref_addprops(node) -> bool: |
| 62 | + """type:object with additionalProperties: $ref → keep non-nullable.""" |
| 63 | + if not isinstance(node, dict): |
| 64 | + return False |
| 65 | + t = node.get("type") |
| 66 | + is_object = t == "object" or (isinstance(t, list) and "object" in t) |
| 67 | + if not is_object: |
| 68 | + return False |
| 69 | + ap = node.get("additionalProperties") |
| 70 | + return isinstance(ap, dict) and "$ref" in ap |
| 71 | + |
| 72 | + |
| 73 | +NULLABLE_SCALAR_OR_ARRAY = {"array", "integer", "number", "string", "boolean"} |
| 74 | + |
| 75 | + |
| 76 | +def walk_schema(node) -> None: |
| 77 | + if not isinstance(node, dict): |
| 78 | + return |
| 79 | + own_required = set(node.get("required") or []) |
| 80 | + if "properties" in node and isinstance(node["properties"], dict): |
| 81 | + for pname, pnode in node["properties"].items(): |
| 82 | + if isinstance(pnode, dict) and pname not in own_required: |
| 83 | + t = pnode.get("type") |
| 84 | + if is_object_with_ref_addprops(pnode): |
| 85 | + pass |
| 86 | + elif isinstance(t, str) and t in NULLABLE_SCALAR_OR_ARRAY: |
| 87 | + make_nullable(pnode) |
| 88 | + elif t == "object": |
| 89 | + make_nullable(pnode) |
| 90 | + elif isinstance(t, list): |
| 91 | + if "null" not in t and any( |
| 92 | + x in NULLABLE_SCALAR_OR_ARRAY | {"object"} for x in t |
| 93 | + ): |
| 94 | + t.append("null") |
| 95 | + walk_schema(pnode) |
| 96 | + for k in ("allOf", "oneOf", "anyOf"): |
| 97 | + if k in node and isinstance(node[k], list): |
| 98 | + for sub in node[k]: |
| 99 | + walk_schema(sub) |
| 100 | + if "items" in node and isinstance(node["items"], dict): |
| 101 | + walk_schema(node["items"]) |
| 102 | + if "additionalProperties" in node and isinstance(node["additionalProperties"], dict): |
| 103 | + walk_schema(node["additionalProperties"]) |
| 104 | + |
| 105 | + |
| 106 | +def convert_existing_nullable(node) -> None: |
| 107 | + if isinstance(node, dict): |
| 108 | + if node.get("nullable") is True: |
| 109 | + make_nullable(node) |
| 110 | + for v in list(node.values()): |
| 111 | + convert_existing_nullable(v) |
| 112 | + elif isinstance(node, list): |
| 113 | + for v in node: |
| 114 | + convert_existing_nullable(v) |
| 115 | + |
| 116 | + |
| 117 | +def main() -> int: |
| 118 | + spec_path = Path(sys.argv[1]) if len(sys.argv) > 1 else find_spec_path() |
| 119 | + if not spec_path.exists(): |
| 120 | + print(f"spec not found: {spec_path}", file=sys.stderr) |
| 121 | + return 1 |
| 122 | + |
| 123 | + text = spec_path.read_text() |
| 124 | + text = text.replace("format: date-time", "format: dateTime") |
| 125 | + spec_path.write_text(text) |
| 126 | + |
| 127 | + yaml = YAML() |
| 128 | + yaml.preserve_quotes = True |
| 129 | + yaml.width = 4096 |
| 130 | + yaml.indent(mapping=2, sequence=4, offset=2) |
| 131 | + |
| 132 | + with spec_path.open() as f: |
| 133 | + doc = yaml.load(f) |
| 134 | + |
| 135 | + schemas = doc["components"]["schemas"] |
| 136 | + for sch in schemas.values(): |
| 137 | + walk_schema(sch) |
| 138 | + |
| 139 | + convert_existing_nullable(doc) |
| 140 | + |
| 141 | + for s in ("HostConfig", "PortMap"): |
| 142 | + if s in schemas: |
| 143 | + make_nullable(schemas[s]) |
| 144 | + |
| 145 | + with spec_path.open("w") as f: |
| 146 | + yaml.dump(doc, f) |
| 147 | + |
| 148 | + print(f"patched {spec_path}") |
| 149 | + return 0 |
| 150 | + |
| 151 | + |
| 152 | +if __name__ == "__main__": |
| 153 | + sys.exit(main()) |
0 commit comments