Skip to content

Commit e0f57d9

Browse files
committed
LCORE-2755: schema dumper refactoring
1 parent 1e60f35 commit e0f57d9

3 files changed

Lines changed: 42 additions & 55 deletions

File tree

src/utils/models_dumper.py

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import json
44

5-
from pydantic.json_schema import models_json_schema
6-
75
import models.compaction as models_compaction
8-
from utils.json_schema_updater import recursive_update
6+
from utils.openapi_schema_dumper import dump_openapi_schema
97

108

119
def dump_models(filename: str) -> None:
@@ -23,29 +21,5 @@ def dump_models(filename: str) -> None:
2321
------
2422
IOError: If the file cannot be written.
2523
"""
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)
24+
models = [models_compaction.ConversationSummary]
25+
dump_openapi_schema(models, filename)

src/utils/openapi_schema_dumper.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import json
2+
3+
from pydantic.json_schema import models_json_schema
4+
from utils.json_schema_updater import recursive_update
5+
6+
7+
def dump_openapi_schema(models: list, filename: str) -> None:
8+
"""Write an OpenAPI-compatible JSON schema for the given models to a file.
9+
10+
Parameters:
11+
----------
12+
- models: list - Pydantic model classes to include in the schema
13+
- filename: str - name of file to export the schema to
14+
15+
Raises:
16+
------
17+
IOError: If the file cannot be written.
18+
"""
19+
with open(filename, "w", encoding="utf-8") as fout:
20+
_, schemas = models_json_schema(
21+
[(model, "validation") for model in models],
22+
ref_template="`#/components/schemas/`{model}",
23+
)
24+
schemas = recursive_update(schemas)
25+
openapi_schema = {
26+
"openapi": "3.0.0",
27+
"info": {
28+
"title": "Lightspeed Core Stack",
29+
"version": "0.3.0",
30+
},
31+
"components": {
32+
"schemas": schemas.get("$defs", {}),
33+
},
34+
"paths": {},
35+
}
36+
json.dump(openapi_schema, fout, indent=4)

src/utils/schema_dumper.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
import json
44

5-
from pydantic.json_schema import models_json_schema
6-
75
from models.config import Configuration
8-
from utils.json_schema_updater import recursive_update
6+
from utils.openapi_schema_dumper import dump_openapi_schema
97

108

119
def dump_schema(filename: str) -> None:
@@ -23,26 +21,5 @@ def dump_schema(filename: str) -> None:
2321
------
2422
IOError: If the file cannot be written.
2523
"""
26-
with open(filename, "w", encoding="utf-8") as fout:
27-
# retrieve the schema
28-
_, schemas = models_json_schema(
29-
[(model, "validation") for model in [Configuration]],
30-
ref_template="#/components/schemas/{model}",
31-
)
32-
33-
# fix the schema
34-
schemas = recursive_update(schemas)
35-
36-
# add all required metadata
37-
openapi_schema = {
38-
"openapi": "3.0.0",
39-
"info": {
40-
"title": "Lightspeed Core Stack",
41-
"version": "0.3.0",
42-
},
43-
"components": {
44-
"schemas": schemas.get("$defs", {}),
45-
},
46-
"paths": {},
47-
}
48-
json.dump(openapi_schema, fout, indent=4)
24+
models = [Configuration]
25+
dump_openapi_schema(models, filename)

0 commit comments

Comments
 (0)