From dc304a48be7edfde12c446da1ee42614bfb9709e Mon Sep 17 00:00:00 2001 From: Alejandro Villar Date: Thu, 10 Oct 2024 02:32:41 +0200 Subject: [PATCH] Fix for reassigned term aliases (#2925) --- rdflib/plugins/shared/jsonld/context.py | 5 +++ test/jsonld/test_reassign_id.py | 54 +++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 test/jsonld/test_reassign_id.py diff --git a/rdflib/plugins/shared/jsonld/context.py b/rdflib/plugins/shared/jsonld/context.py index 1eec94419..e6b668878 100644 --- a/rdflib/plugins/shared/jsonld/context.py +++ b/rdflib/plugins/shared/jsonld/context.py @@ -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 diff --git a/test/jsonld/test_reassign_id.py b/test/jsonld/test_reassign_id.py new file mode 100644 index 000000000..97a723dbc --- /dev/null +++ b/test/jsonld/test_reassign_id.py @@ -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 = """ + _:b0 . + _:b1 . +_:b0 "item1" . +_:b1 "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)