Skip to content

Commit dd60b56

Browse files
committed
fix/add_optional_to_model
- Fixed the nested model automatically generation.
1 parent 810d7fb commit dd60b56

3 files changed

Lines changed: 7 additions & 18 deletions

File tree

CHANGELOG.txt

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
Change Log
22
===============
3-
VERSION="0.0.21"
3+
VERSION="0.0.22"
44
LAST_UPDATE="11/02/2026"
55
------------------
6-
- Added a mechanism to automatically get the expected output
7-
from the ontology and add the expected response schema.
8-
- Added to the example the updates required to test the new
9-
feature of response schema.
6+
- Fixed the nested model automatically generation.
107
------------------

OntologyToAPI/core/Utility.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import logging
66
from pathlib import Path
77
from pydantic import BaseModel, create_model, ConfigDict
8-
from typing import Any, List, Dict, Union
8+
from typing import Any, List, Dict, Union, Optional
99
import rdflib
1010
from Settings import auto_config as cfg
1111

@@ -97,12 +97,12 @@ def generate_pydantic(model_name: str, node_dict: Dict) -> type[BaseModel]:
9797
if children:
9898
sub_model = generate_pydantic(f"{field_name}Model", children)
9999
if field_type_str == 'object_list':
100-
fields[field_name] = (List[sub_model], ...)
100+
fields[field_name] = (Optional[List[sub_model]], None)
101101
else:
102-
fields[field_name] = (sub_model, ...)
102+
fields[field_name] = (Optional[sub_model], None)
103103
else:
104104
python_type = TYPE_MAP.get(field_type_str, Any)
105-
fields[field_name] = (python_type, ...)
105+
fields[field_name] = (Optional[python_type], None)
106106
return create_model(model_name, **fields, __config__=ConfigDict(extra='allow'))
107107

108108
return generate_pydantic(name, tree)

example/bm.py

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,7 @@ async def convert_temperature_c_to_f(data):
77
"converted": data.params["temperature_c"] * 9 / 5 + 32,
88
"test": {
99
"inner": "lorem ipsum",
10-
},
11-
"test2": [
12-
{
13-
"inner1": "lorem ipsum 1",
14-
},
15-
{
16-
"inner1": "lorem ipsum 2",
17-
}
18-
]
10+
}
1911
}
2012
else:
2113
return {

0 commit comments

Comments
 (0)