Skip to content

Commit

Permalink
Handle attributes edge case in schema_as_dict
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Chevalier committed Aug 3, 2024
1 parent 81b3c62 commit 3d8de35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion linkml_runtime/utils/schema_as_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _remove_names(obj: Any, parent: Optional[str]) -> Any:
:return:
"""
if isinstance(obj, dict):
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']}
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']}
elif isinstance(obj, list):
return [_remove_names(x, parent) for x in obj]
else:
Expand Down
27 changes: 27 additions & 0 deletions tests/test_utils/test_schema_as_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from linkml_runtime.loaders.yaml_loader import YAMLLoader
from linkml_runtime.utils.schema_as_dict import schema_as_yaml_dump, schema_as_dict
from linkml_runtime.utils.schemaview import SchemaView
from linkml_runtime.utils.schema_builder import ClassDefinition, SchemaBuilder, SlotDefinition

from tests.test_utils import INPUT_DIR, OUTPUT_DIR

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


def test_as_dict_with_attributes(self):
"""
tests schema_as_dict, see https://github.com/linkml/linkml/issues/100
"""

# Create a class with an attribute named 'name'
cls = ClassDefinition(name="Patient")
slots = [
SlotDefinition(name="id", range="string"),
SlotDefinition(name="name", range="string"),
]
builder = SchemaBuilder()
builder.add_class(cls=cls, slots=slots, use_attributes=True)

# Verify that the 'name' slot exists in the schema
view = SchemaView(builder.schema)
self.assertIn('name', view.all_slots())

# Convert the schema to a dict
obj = schema_as_dict(view.schema)

# Verify that the 'name' slot still exists, as an attribute
self.assertIn('name', obj['classes']['Patient']['attributes'])


if __name__ == '__main__':
unittest.main()

0 comments on commit 3d8de35

Please sign in to comment.