Skip to content

Commit df84c27

Browse files
authored
Merge pull request #2090 from tisnik/lcore-2755-initial-refactoring
LCORE-2755: initial refactoring of schema dumper
2 parents 704d59f + 2a10e35 commit df84c27

4 files changed

Lines changed: 519 additions & 461 deletions

File tree

‎src/utils/json_schema_updater.py‎

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

‎src/utils/schema_dumper.py‎

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,11 @@
11
"""Function to dump the configuration schema into OpenAPI-compatible format."""
22

33
import json
4-
from typing import Any
54

65
from pydantic.json_schema import models_json_schema
76

87
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
619

6210

6311
def dump_schema(filename: str) -> None:

0 commit comments

Comments
 (0)