diff --git a/rdflib/plugins/parsers/jsonld.py b/rdflib/plugins/parsers/jsonld.py index bc4585624..45e696adb 100644 --- a/rdflib/plugins/parsers/jsonld.py +++ b/rdflib/plugins/parsers/jsonld.py @@ -34,6 +34,7 @@ # we should consider streaming the input to deal with arbitrarily large graphs. from __future__ import annotations +import secrets import warnings from collections.abc import Iterable from typing import TYPE_CHECKING, Any, Union @@ -215,6 +216,7 @@ def __init__( if allow_lists_of_lists is not None else ALLOW_LISTS_OF_LISTS ) + self.invalid_uri_to_bnode: dict[str, BNode] = {} def parse(self, data: Any, context: Context, dataset: Graph) -> Graph: topcontext = False @@ -623,7 +625,12 @@ def _to_rdf_id(self, context: Context, id_val: str) -> IdentifiedNode | None: uri = context.resolve(id_val) if not self.generalized_rdf and ":" not in uri: return None - return URIRef(uri) + node: IdentifiedNode = URIRef(uri) + if not str(node): + if id_val not in self.invalid_uri_to_bnode: + self.invalid_uri_to_bnode[id_val] = BNode(secrets.token_urlsafe(20)) + node = self.invalid_uri_to_bnode[id_val] + return node def _get_bnodeid(self, ref: str) -> str | None: if not ref.startswith("_:"): diff --git a/test/jsonld/local-suite/manifest.jsonld b/test/jsonld/local-suite/manifest.jsonld index b32fd059a..0150b44c7 100644 --- a/test/jsonld/local-suite/manifest.jsonld +++ b/test/jsonld/local-suite/manifest.jsonld @@ -27,6 +27,17 @@ "purpose": "Multiple @id aliases. Issue #2164", "input": "toRdf-twoimports-in.jsonld", "expect": "toRdf-twoimports-out.nq" + }, + { + "@id": "#toRdf-two-invalid-ids", + "@type": ["jld:PositiveEvaluationTest", "jld:ToRDFTest"], + "name": "Two invalid identifiers", + "purpose": "Multiple nodes with invalid @ids are not merged together.", + "option": { + "produceGeneralizedRdf": true + }, + "input": "toRdf-twoinvalidids-in.jsonld", + "expect": "toRdf-twoinvalidids-out.nq" } ] } diff --git a/test/jsonld/local-suite/toRdf-twoinvalidids-in.jsonld b/test/jsonld/local-suite/toRdf-twoinvalidids-in.jsonld new file mode 100644 index 000000000..67f62927c --- /dev/null +++ b/test/jsonld/local-suite/toRdf-twoinvalidids-in.jsonld @@ -0,0 +1,20 @@ +{ + "@id": "https://example.org/root-object", + "https://schema.org/author": [ + { + "@id": "https://example.org/ invalid url 1", + "https://schema.org/name": "Jane Doe" + }, + { + "@id": "https://example.org/ invalid url 1", + "https://schema.org/givenName": "Jane", + "https://schema.org/familyName": "Doe" + }, + { + "@id": "https://example.org/ invalid url 2", + "https://schema.org/name": "John Doe", + "https://schema.org/givenName": "John", + "https://schema.org/familyName": "Doe" + } + ] +} diff --git a/test/jsonld/local-suite/toRdf-twoinvalidids-out.nq b/test/jsonld/local-suite/toRdf-twoinvalidids-out.nq new file mode 100644 index 000000000..c6550560c --- /dev/null +++ b/test/jsonld/local-suite/toRdf-twoinvalidids-out.nq @@ -0,0 +1,10 @@ + + _:b1. + _:b2. + +_:b1 "Jane Doe". +_:b1 "Jane". +_:b1 "Doe". +_:b2 "John Doe". +_:b2 "John". +_:b2 "Doe".