Skip to content
Merged
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
9 changes: 8 additions & 1 deletion rdflib/plugins/parsers/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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("_:"):
Expand Down
11 changes: 11 additions & 0 deletions test/jsonld/local-suite/manifest.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}
20 changes: 20 additions & 0 deletions test/jsonld/local-suite/toRdf-twoinvalidids-in.jsonld
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
10 changes: 10 additions & 0 deletions test/jsonld/local-suite/toRdf-twoinvalidids-out.nq
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<https://example.org/root-object> <https://schema.org/author> _:b1.
<https://example.org/root-object> <https://schema.org/author> _:b2.

_:b1 <https://schema.org/name> "Jane Doe".
_:b1 <https://schema.org/givenName> "Jane".
_:b1 <https://schema.org/familyName> "Doe".
_:b2 <https://schema.org/name> "John Doe".
_:b2 <https://schema.org/givenName> "John".
_:b2 <https://schema.org/familyName> "Doe".
Loading