Skip to content

Commit dcbe601

Browse files
authored
Merge pull request lightspeed-core#2109 from tisnik/lcore-2755-dumper-refactoring
LCORE-2755: schema and models dumper refactoring
2 parents 14da408 + e4f4b26 commit dcbe601

5 files changed

Lines changed: 36 additions & 50 deletions

File tree

src/lightspeed_stack.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from log import get_logger, setup_logging
1515
from runners.quota_scheduler import start_quota_scheduler
1616
from runners.uvicorn import start_uvicorn
17-
from utils import models_dumper, schema_dumper
17+
from utils import config_dumper, models_dumper
1818

1919
setup_logging()
2020
logger = get_logger(__name__)
@@ -195,7 +195,7 @@ def main() -> None:
195195
# into a JSON file that is compatible with OpenAPI schema specification
196196
if args.dump_schema:
197197
try:
198-
schema_dumper.dump_schema("schema.json")
198+
config_dumper.dump_schema("schema.json")
199199
logger.info("Configuration schema dumped to schema.json")
200200
except Exception as e:
201201
logger.error("Failed to dump configuration schema: %s", e)

src/utils/config_dumper.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Function to dump the configuration schema into OpenAPI-compatible format."""
2+
3+
from models.config import Configuration
4+
from utils.openapi_schema_dumper import dump_openapi_schema
5+
6+
7+
def dump_schema(filename: str) -> None:
8+
"""Dump the configuration schema into OpenAPI-compatible JSON file.
9+
10+
Parameters:
11+
----------
12+
- filename: str - name of file to export the schema to
13+
14+
Returns:
15+
-------
16+
- None
17+
18+
Raises:
19+
------
20+
IOError: If the file cannot be written.
21+
"""
22+
models = [Configuration]
23+
dump_openapi_schema(models, filename)

src/utils/models_dumper.py

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
"""Function to dump the schema of all data models into OpenAPI-compatible format."""
22

3-
import json
4-
5-
from pydantic.json_schema import models_json_schema
6-
73
import models.compaction as models_compaction
8-
from utils.json_schema_updater import recursive_update
4+
from utils.openapi_schema_dumper import dump_openapi_schema
95

106

117
def dump_models(filename: str) -> None:
@@ -23,29 +19,5 @@ def dump_models(filename: str) -> None:
2319
------
2420
IOError: If the file cannot be written.
2521
"""
26-
with open(filename, "w", encoding="utf-8") as fout:
27-
# retrieve the schema
28-
_, schemas = models_json_schema(
29-
[
30-
(model, "validation")
31-
for model in [models_compaction.ConversationSummary]
32-
],
33-
ref_template="#/components/schemas/{model}",
34-
)
35-
36-
# fix the schema
37-
schemas = recursive_update(schemas)
38-
39-
# add all required metadata
40-
openapi_schema = {
41-
"openapi": "3.0.0",
42-
"info": {
43-
"title": "Lightspeed Core Stack",
44-
"version": "0.3.0",
45-
},
46-
"components": {
47-
"schemas": schemas.get("$defs", {}),
48-
},
49-
"paths": {},
50-
}
51-
json.dump(openapi_schema, fout, indent=4)
22+
models = [models_compaction.ConversationSummary]
23+
dump_openapi_schema(models, filename)
Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,30 @@
1-
"""Function to dump the configuration schema into OpenAPI-compatible format."""
1+
"""Utility function to dump schema with list of models into OpenAPI-compatible JSON format."""
22

33
import json
44

55
from pydantic.json_schema import models_json_schema
66

7-
from models.config import Configuration
87
from utils.json_schema_updater import recursive_update
98

109

11-
def dump_schema(filename: str) -> None:
12-
"""Dump the configuration schema into OpenAPI-compatible JSON file.
10+
def dump_openapi_schema(models: list, filename: str) -> None:
11+
"""Write an OpenAPI-compatible JSON schema for the given models to a file.
1312
1413
Parameters:
1514
----------
15+
- models: list - Pydantic model classes to include in the schema
1616
- filename: str - name of file to export the schema to
1717
18-
Returns:
19-
-------
20-
- None
21-
2218
Raises:
2319
------
2420
IOError: If the file cannot be written.
2521
"""
2622
with open(filename, "w", encoding="utf-8") as fout:
27-
# retrieve the schema
2823
_, schemas = models_json_schema(
29-
[(model, "validation") for model in [Configuration]],
30-
ref_template="#/components/schemas/{model}",
24+
[(model, "validation") for model in models],
25+
ref_template="`#/components/schemas/`{model}",
3126
)
32-
33-
# fix the schema
3427
schemas = recursive_update(schemas)
35-
36-
# add all required metadata
3728
openapi_schema = {
3829
"openapi": "3.0.0",
3930
"info": {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
"""Unit tests for utils/schema_dumper module."""
1+
"""Unit tests for utils/config_dumper module."""
22

33
from json import load
44
from pathlib import Path
55

6-
from utils.schema_dumper import dump_schema
6+
from utils.config_dumper import dump_schema
77

88

99
def test_dump_schema(tmpdir: Path) -> None:

0 commit comments

Comments
 (0)