Skip to content

Fix Schemaview.get_uri(...) for no default_prefix in schema #378

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions linkml_runtime/utils/schemaview.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from linkml_runtime.utils.namespaces import Namespaces
from deprecated.classic import deprecated
from linkml_runtime.utils.context_utils import parse_import_map, map_import
from linkml_runtime.utils.formatutils import is_empty, underscore, camelcase
from linkml_runtime.utils.formatutils import camelcase, is_empty, sfx, underscore
from linkml_runtime.utils.pattern import PatternResolver
from linkml_runtime.linkml_model.meta import *
from linkml_runtime.exceptions import OrderingError
Expand Down Expand Up @@ -1121,13 +1121,18 @@ def get_uri(self, element: Union[ElementName, Element], imports=True, expand=Fal
raise ValueError(f'Cannot find {e.from_schema} in schema_map')
else:
schema = self.schema_map[self.in_schema(e.name)]
pfx = schema.default_prefix
if use_element_type:
e_type = e.class_name.split("_",1)[0] # for example "class_definition"
e_type_path = f"{e_type}/"
else:
e_type_path = ""
uri = f'{pfx}:{e_type_path}{e_name}'
pfx = schema.default_prefix
# To construct the uri we have to find out if the schema has a default_prefix
# or if a pseudo "prefix" was derived from the schema id.
if pfx == sfx(str(schema.id)): # no prefix defined in schema
uri = f'{pfx}{e_type_path}{e_name}'
else:
uri = f'{pfx}:{e_type_path}{e_name}'
if expand:
return self.expand_curie(uri)
else:
Expand Down
31 changes: 15 additions & 16 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/test_utils/test_schemaview.py
Original file line number Diff line number Diff line change
Expand Up @@ -923,3 +923,17 @@ def test_type_and_slot_with_same_name():
view.add_type(TypeDefinition(name="test", from_schema="https://example.org/imported#"))

assert view.get_uri("test", imports=True) == "ex:test"


def test_uris_without_default_prefix():
"""
Test if uri is correct if no default_prefix is defined for the schema. Issue: linkml/linkml#2578
"""
schema_definition = SchemaDefinition(id="https://example.org/test#", name="test_schema")

view = SchemaView(schema_definition)
view.add_class(ClassDefinition(name="TestClass", from_schema="https://example.org/another#"))
view.add_slot(SlotDefinition(name="test_slot", from_schema="https://example.org/another#"))

assert view.get_uri("TestClass", imports=True) == "https://example.org/test#TestClass"
assert view.get_uri("test_slot", imports=True) == "https://example.org/test#test_slot"
Loading