Skip to content

Commit c612c5a

Browse files
committed
Fix get_uri for no default_prefix in schema
1 parent 905789c commit c612c5a

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

linkml_runtime/utils/schemaview.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,13 +1121,18 @@ def get_uri(self, element: Union[ElementName, Element], imports=True, expand=Fal
11211121
raise ValueError(f'Cannot find {e.from_schema} in schema_map')
11221122
else:
11231123
schema = self.schema_map[self.in_schema(e.name)]
1124-
pfx = schema.default_prefix
11251124
if use_element_type:
11261125
e_type = e.class_name.split("_",1)[0] # for example "class_definition"
11271126
e_type_path = f"{e_type}/"
11281127
else:
11291128
e_type_path = ""
1130-
uri = f'{pfx}:{e_type_path}{e_name}'
1129+
pfx = schema.default_prefix
1130+
# To construct the uri we have to find out if the schema has a default_prefix
1131+
# or if a pseudo "prefix" was derived from the schema id.
1132+
if pfx == sfx(str(schema.id)): # no prefix defined in schema
1133+
uri = f'{pfx}{e_type_path}{e_name}'
1134+
else:
1135+
uri = f'{pfx}:{e_type_path}{e_name}'
11311136
if expand:
11321137
return self.expand_curie(uri)
11331138
else:

tests/test_utils/test_schemaview.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,7 +488,6 @@ def test_imports(view):
488488
height = view.get_slot('height_in_m')
489489
assert height.unit.ucum_code == "m"
490490

491-
492491
def test_imports_from_schemaview(view):
493492
"""view should by default dynamically include imports chain"""
494493
view2 = SchemaView(view.schema)
@@ -923,3 +922,17 @@ def test_type_and_slot_with_same_name():
923922
view.add_type(TypeDefinition(name="test", from_schema="https://example.org/imported#"))
924923

925924
assert view.get_uri("test", imports=True) == "ex:test"
925+
926+
927+
def test_uris_without_default_prefix():
928+
"""
929+
Test if uri is correct if no default_prefix is defined for the schema. Issue: linkml/linkml#2578
930+
"""
931+
schema_definition = SchemaDefinition(id="https://example.org/test#", name="test_schema")
932+
933+
view = SchemaView(schema_definition)
934+
view.add_class(ClassDefinition(name="TestClass", from_schema="https://example.org/another#"))
935+
view.add_slot(SlotDefinition(name="test_slot", from_schema="https://example.org/another#"))
936+
937+
assert view.get_uri("TestClass", imports=True) == "https://example.org/test#TestClass"
938+
assert view.get_uri("test_slot", imports=True) == "https://example.org/test#test_slot"

0 commit comments

Comments
 (0)