Skip to content

Commit 641bb1c

Browse files
authored
Merge pull request #336 from bob-chevalier/attributes-schema-as-dict
Handle attributes edge case in schema_as_dict
2 parents 2271044 + 3d8de35 commit 641bb1c

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

linkml_runtime/utils/schema_as_dict.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def _remove_names(obj: Any, parent: Optional[str]) -> Any:
4242
:return:
4343
"""
4444
if isinstance(obj, dict):
45-
return {k: _remove_names(v, k) for k, v in obj.items() if k != 'name' or parent is None or parent in ['slots', 'slot_usage']}
45+
return {k: _remove_names(v, k) for k, v in obj.items() if k != 'name' or parent is None or parent in ['slots', 'slot_usage', 'attributes']}
4646
elif isinstance(obj, list):
4747
return [_remove_names(x, parent) for x in obj]
4848
else:

tests/test_utils/test_schema_as_dict.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from linkml_runtime.loaders.yaml_loader import YAMLLoader
88
from linkml_runtime.utils.schema_as_dict import schema_as_yaml_dump, schema_as_dict
99
from linkml_runtime.utils.schemaview import SchemaView
10+
from linkml_runtime.utils.schema_builder import ClassDefinition, SchemaBuilder, SlotDefinition
1011

1112
from tests.test_utils import INPUT_DIR, OUTPUT_DIR
1213

@@ -45,5 +46,31 @@ def test_as_dict(self):
4546
assert 'text' not in pv
4647
self.assertIn('name', obj['slots'])
4748

49+
50+
def test_as_dict_with_attributes(self):
51+
"""
52+
tests schema_as_dict, see https://github.com/linkml/linkml/issues/100
53+
"""
54+
55+
# Create a class with an attribute named 'name'
56+
cls = ClassDefinition(name="Patient")
57+
slots = [
58+
SlotDefinition(name="id", range="string"),
59+
SlotDefinition(name="name", range="string"),
60+
]
61+
builder = SchemaBuilder()
62+
builder.add_class(cls=cls, slots=slots, use_attributes=True)
63+
64+
# Verify that the 'name' slot exists in the schema
65+
view = SchemaView(builder.schema)
66+
self.assertIn('name', view.all_slots())
67+
68+
# Convert the schema to a dict
69+
obj = schema_as_dict(view.schema)
70+
71+
# Verify that the 'name' slot still exists, as an attribute
72+
self.assertIn('name', obj['classes']['Patient']['attributes'])
73+
74+
4875
if __name__ == '__main__':
4976
unittest.main()

0 commit comments

Comments
 (0)