|
1 | 1 | """Function to dump the configuration schema into OpenAPI-compatible format.""" |
2 | 2 |
|
3 | 3 | import json |
4 | | -from typing import Any |
5 | 4 |
|
6 | 5 | from pydantic.json_schema import models_json_schema |
7 | 6 |
|
8 | 7 | from models.config import Configuration |
9 | | - |
10 | | - |
11 | | -# pylint: disable=too-many-boolean-expressions |
12 | | -def recursive_update( |
13 | | - original: dict[str, Any], |
14 | | -) -> dict[str, Any]: |
15 | | - """Transform a JSON Schema-like dictionary into an OpenAPI-compatible schema. |
16 | | -
|
17 | | - Recursively walks the input mapping and applies compatibility fixes: |
18 | | - - converts patterns like `anyOf: [{ "type": X }, { "type": "null" }]` into |
19 | | - a single `"type": X` with `"nullable": True` (optional types) |
20 | | - - rewrites `"exclusiveMinimum"` to `"minimum"` |
21 | | - - otherwise preserves entries |
22 | | -
|
23 | | - Parameters: |
24 | | - ---------- |
25 | | - original (dict[str, Any]): Input schema dictionary that is produced by |
26 | | - Pydantic. |
27 | | -
|
28 | | - Returns: |
29 | | - ------- |
30 | | - dict[str, Any]: A new schema dictionary with all OpenAPI-compatible |
31 | | - transformations applied. |
32 | | - """ |
33 | | - new: dict[str, Any] = {} |
34 | | - for key, value in original.items(): |
35 | | - # recurse into sub-dictionaries |
36 | | - if isinstance(value, dict): |
37 | | - new[key] = recursive_update(value) |
38 | | - # optional types fixes |
39 | | - elif ( |
40 | | - key == "anyOf" |
41 | | - and isinstance(value, list) |
42 | | - and len(value) >= 2 |
43 | | - and isinstance(value[0], dict) |
44 | | - and "type" in value[0] |
45 | | - and isinstance(value[1], dict) |
46 | | - and value[1].get("type") == "null" |
47 | | - ): |
48 | | - # only the first type is correct, |
49 | | - # we need to ignore the second one |
50 | | - val = value[0]["type"] |
51 | | - new["type"] = val |
52 | | - # create new attribute |
53 | | - new["nullable"] = True |
54 | | - # exclusiveMinimum attribute handling is broken |
55 | | - # in Pydantic - this is simple fix |
56 | | - elif key == "exclusiveMinimum": |
57 | | - new["minimum"] = value |
58 | | - else: |
59 | | - new[key] = value |
60 | | - return new |
| 8 | +from utils.json_schema_updater import recursive_update |
61 | 9 |
|
62 | 10 |
|
63 | 11 | def dump_schema(filename: str) -> None: |
|
0 commit comments