Skip to content

Commit 78b15d7

Browse files
authored
fix subdocument in from_json_schema (#314)
* fix subdocument in from_json_schema * shed the code
1 parent f2817a4 commit 78b15d7

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

terminusdb_client/tests/test_woqlSchema.py

+39
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,45 @@ def test_compress_data(patched, patched2, patched3, patched4):
361361
)
362362

363363

364+
def test_from_json_schema():
365+
test_json_schema = {
366+
"type": "object",
367+
"properties": {
368+
"first_name": {"type": "string"},
369+
"last_name": {"type": "string"},
370+
"birthday": {"type": "string", "format": "date"},
371+
"address": {
372+
"type": "object",
373+
"properties": {
374+
"street_address": {"type": "string"},
375+
"city": {"type": "string"},
376+
"state": {"type": "string"},
377+
"country": {"type": "string"},
378+
},
379+
},
380+
},
381+
}
382+
383+
schema = WOQLSchema()
384+
test_result = schema.from_json_schema("TestJSON", test_json_schema, pipe=True)
385+
assert test_result == {
386+
"@id": "TestJSON",
387+
"@type": "Class",
388+
"first_name": "xsd:string",
389+
"last_name": "xsd:string",
390+
"birthday": "xsd:string",
391+
"address": {
392+
"@id": "address",
393+
"@type": "Class",
394+
"@subdocument": [],
395+
"street_address": "xsd:string",
396+
"city": "xsd:string",
397+
"state": "xsd:string",
398+
"country": "xsd:string",
399+
},
400+
}
401+
402+
364403
# def test_schema_delete():
365404
# other_schema.delete_property(Title)
366405
# assert other_schema.all_obj() == {Employee, Address, Team}

terminusdb_client/woqlschema/woql_schema.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,20 @@ def convert_property(prop_name, prop):
734734
# it's datetime
735735
if "format" in prop and prop["format"] == "date-time":
736736
return "xsd:dataTime"
737-
# it's another object
737+
# it's a subdocument
738+
elif prop.get("type") is not None and prop["type"] == "object":
739+
if prop.get("properties") is None:
740+
raise RuntimeError(
741+
f"subdocument {prop_name} not in proper format: 'properties' is missing"
742+
)
743+
sub_dict = {"@id": prop_name, "@type": "Class", "@subdocument": []}
744+
for sub_prop_name, sub_prop in prop["properties"].items():
745+
sub_dict[sub_prop_name] = convert_property(sub_prop_name, sub_prop)
746+
if pipe: # end of journey for pipemode
747+
return sub_dict
748+
self._contruct_class(sub_dict)
749+
return prop_name
750+
# it's another document
738751
elif prop.get("type") is None and prop.get("$ref") is not None:
739752
prop_type = prop["$ref"].split("/")[-1]
740753
if defs is None or prop_type not in defs:

0 commit comments

Comments
 (0)