Skip to content

Commit

Permalink
Fix for reassigned term aliases (#2925)
Browse files Browse the repository at this point in the history
  • Loading branch information
avillar authored Oct 10, 2024
1 parent 0b69f4f commit dc304a4
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions rdflib/plugins/shared/jsonld/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ def _read_term(

if idref in NODE_KEYS:
self._alias.setdefault(idref, []).append(name)
else:
# undo aliases that may have been inherited from parent context
for v in self._alias.values():
if name in v:
v.remove(name)

def _rec_expand(
self, source: Dict[str, Any], expr: Optional[str], prev: Optional[str] = None
Expand Down
54 changes: 54 additions & 0 deletions test/jsonld/test_reassign_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from __future__ import annotations

from rdflib import BNode, Graph, Literal, Namespace, URIRef

DATA = """
{
"@context": {
"@version": 1.1,
"ex": "https://example.com/",
"@base": "https://example.com/res/",
"id": "@id",
"test": {
"@id": "ex:test",
"@context": {
"id": "ex:id"
}
}
},
"id": "parent",
"test": [
{ "id": "item1" },
{ "id": "item2" }
]
}
"""

DATA_OK = """
<https://example.com/res/parent> <https://example.com/test> _:b0 .
<https://example.com/res/parent> <https://example.com/test> _:b1 .
_:b0 <https://example.com/id> "item1" .
_:b1 <https://example.com/id> "item2" .
"""

EX = Namespace("https://example.com/")


def test_reassign_id():
g = Graph().parse(data=DATA, format="json-ld")
# g = Graph().parse(data=DATA_OK)

parent = URIRef("https://example.com/res/parent")
ex_id = EX.id
ex_test = EX.test

objects = list(g.objects(parent, ex_test))

assert len(g) == 4
assert len(objects) == 2
for obj in objects:
assert isinstance(obj, BNode)
obj_pred_objects = list(g.predicate_objects(obj))
assert len(obj_pred_objects) == 1
assert obj_pred_objects[0][0] == ex_id
assert isinstance(obj_pred_objects[0][1], Literal)

0 comments on commit dc304a4

Please sign in to comment.