Skip to content

Commit dc304a4

Browse files
authored
Fix for reassigned term aliases (#2925)
1 parent 0b69f4f commit dc304a4

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

rdflib/plugins/shared/jsonld/context.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,11 @@ def _read_term(
580580

581581
if idref in NODE_KEYS:
582582
self._alias.setdefault(idref, []).append(name)
583+
else:
584+
# undo aliases that may have been inherited from parent context
585+
for v in self._alias.values():
586+
if name in v:
587+
v.remove(name)
583588

584589
def _rec_expand(
585590
self, source: Dict[str, Any], expr: Optional[str], prev: Optional[str] = None

test/jsonld/test_reassign_id.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from __future__ import annotations
2+
3+
from rdflib import BNode, Graph, Literal, Namespace, URIRef
4+
5+
DATA = """
6+
{
7+
"@context": {
8+
"@version": 1.1,
9+
"ex": "https://example.com/",
10+
"@base": "https://example.com/res/",
11+
"id": "@id",
12+
"test": {
13+
"@id": "ex:test",
14+
"@context": {
15+
"id": "ex:id"
16+
}
17+
}
18+
},
19+
"id": "parent",
20+
"test": [
21+
{ "id": "item1" },
22+
{ "id": "item2" }
23+
]
24+
}
25+
"""
26+
27+
DATA_OK = """
28+
<https://example.com/res/parent> <https://example.com/test> _:b0 .
29+
<https://example.com/res/parent> <https://example.com/test> _:b1 .
30+
_:b0 <https://example.com/id> "item1" .
31+
_:b1 <https://example.com/id> "item2" .
32+
"""
33+
34+
EX = Namespace("https://example.com/")
35+
36+
37+
def test_reassign_id():
38+
g = Graph().parse(data=DATA, format="json-ld")
39+
# g = Graph().parse(data=DATA_OK)
40+
41+
parent = URIRef("https://example.com/res/parent")
42+
ex_id = EX.id
43+
ex_test = EX.test
44+
45+
objects = list(g.objects(parent, ex_test))
46+
47+
assert len(g) == 4
48+
assert len(objects) == 2
49+
for obj in objects:
50+
assert isinstance(obj, BNode)
51+
obj_pred_objects = list(g.predicate_objects(obj))
52+
assert len(obj_pred_objects) == 1
53+
assert obj_pred_objects[0][0] == ex_id
54+
assert isinstance(obj_pred_objects[0][1], Literal)

0 commit comments

Comments
 (0)