|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Preprocess the vendored OpenAPI spec before running datamodel-codegen. |
| 3 | +
|
| 4 | +Applies structural fixes that code generators need: |
| 5 | +1. setRequiredFields — mark non-nullable fields as required |
| 6 | +2. pushRequiredIntoAllOf — propagate required into allOf members |
| 7 | +""" |
| 8 | + |
| 9 | +from __future__ import annotations |
| 10 | + |
| 11 | +import json |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | + |
| 16 | +_REQUEST_PREFIXES = ( |
| 17 | + "Create", "Update", "Add", "Acquire", "Resolve", "Reorder", |
| 18 | + "Test", "Change", "Admin", "Bulk", "Monitor", |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def set_required_fields(spec: dict) -> None: |
| 23 | + schemas = spec.get("components", {}).get("schemas", {}) |
| 24 | + for schema_name, schema in schemas.items(): |
| 25 | + if schema.get("type") != "object" or "properties" not in schema: |
| 26 | + continue |
| 27 | + |
| 28 | + if isinstance(schema.get("required"), list): |
| 29 | + is_req = schema_name.endswith("Request") and schema_name.startswith( |
| 30 | + _REQUEST_PREFIXES |
| 31 | + ) |
| 32 | + if not is_req: |
| 33 | + for prop, prop_schema in schema.get("properties", {}).items(): |
| 34 | + if prop_schema.get("nullable"): |
| 35 | + continue |
| 36 | + if prop in schema["required"]: |
| 37 | + continue |
| 38 | + if prop_schema.get("allOf"): |
| 39 | + continue |
| 40 | + if "default" in prop_schema: |
| 41 | + continue |
| 42 | + schema["required"].append(prop) |
| 43 | + continue |
| 44 | + |
| 45 | + is_request = schema_name.endswith("Request") and schema_name.startswith( |
| 46 | + _REQUEST_PREFIXES |
| 47 | + ) |
| 48 | + if is_request: |
| 49 | + continue |
| 50 | + |
| 51 | + required = [] |
| 52 | + for prop, prop_schema in schema.get("properties", {}).items(): |
| 53 | + if prop_schema.get("nullable"): |
| 54 | + continue |
| 55 | + if prop_schema.get("allOf"): |
| 56 | + continue |
| 57 | + if "default" in prop_schema: |
| 58 | + continue |
| 59 | + required.append(prop) |
| 60 | + if required: |
| 61 | + schema["required"] = required |
| 62 | + |
| 63 | + |
| 64 | +def push_required_into_all_of(spec: dict) -> None: |
| 65 | + schemas = spec.get("components", {}).get("schemas", {}) |
| 66 | + for schema in schemas.values(): |
| 67 | + if not isinstance(schema.get("required"), list): |
| 68 | + continue |
| 69 | + if not isinstance(schema.get("allOf"), list): |
| 70 | + continue |
| 71 | + for member in schema["allOf"]: |
| 72 | + if "properties" not in member: |
| 73 | + continue |
| 74 | + member_required = [f for f in schema["required"] if f in member["properties"]] |
| 75 | + if member_required: |
| 76 | + existing = member.get("required", []) |
| 77 | + member["required"] = list(set(existing + member_required)) |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +# fix_missing_nullable — REMOVED. |
| 82 | +# The root cause (Lombok not copying @Nullable to getters) was fixed in the API |
| 83 | +# by adding `jakarta.annotation.Nullable` to lombok.copyableAnnotations. The |
| 84 | +# generated OpenAPI spec now correctly marks nullable fields via the existing |
| 85 | +# PropertyCustomizer in OpenApiConfig.java. All DTO fields also have explicit |
| 86 | +# @Nullable or @NotNull/@NotBlank annotations, enforced by DtoAnnotationTest. |
| 87 | + |
| 88 | + |
| 89 | +def main() -> None: |
| 90 | + if len(sys.argv) != 3: |
| 91 | + print(f"Usage: {sys.argv[0]} <input.json> <output.json>", file=sys.stderr) |
| 92 | + sys.exit(1) |
| 93 | + |
| 94 | + input_path = Path(sys.argv[1]) |
| 95 | + output_path = Path(sys.argv[2]) |
| 96 | + |
| 97 | + spec = json.loads(input_path.read_text()) |
| 98 | + set_required_fields(spec) |
| 99 | + push_required_into_all_of(spec) |
| 100 | + |
| 101 | + output_path.write_text(json.dumps(spec, indent=2)) |
| 102 | + print(f"Preprocessed: {input_path} -> {output_path}") |
| 103 | + |
| 104 | + |
| 105 | +if __name__ == "__main__": |
| 106 | + main() |
0 commit comments