Skip to content

Commit 767c425

Browse files
authored
Merge pull request #23 from AtomGraph/fix-to-graph-jsonld-array
Fix to graph jsonld array
2 parents b82eabb + 0cec0df commit 767c425

3 files changed

Lines changed: 24 additions & 3 deletions

File tree

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "web-algebra"
3-
version = "1.4.0"
3+
version = "1.4.1"
44
description = "Composable RDF operations in JSON"
55
readme = "README.md"
66
license = "Apache-2.0"

src/web_algebra/operation.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,23 @@ def to_graph(data: Any, *, base: Optional[str] = None) -> Graph:
222222
already a `Graph` (e.g. the output of an upstream op such as CONSTRUCT)
223223
passes through unchanged.
224224
225+
Both shapes of JSON-LD document are accepted: a single node object
226+
(`dict`) and a top-level array of nodes (`list`) — the latter is what
227+
SPARQL DESCRIBE/CONSTRUCT over multiple subjects returns. rdflib's
228+
JSON-LD parser handles both natively.
229+
225230
`base` is the document's base IRI — typically the target URL of the
226231
enclosing op — used to resolve any relative IRIs in the JSON-LD.
227232
"""
228233
if isinstance(data, Graph):
229234
return data
230-
if isinstance(data, dict):
235+
if isinstance(data, (dict, list)):
231236
graph = Graph()
232237
graph.parse(data=json.dumps(data), format="json-ld", publicID=base)
233238
return graph
234239
raise TypeError(
235240
f"Cannot convert {type(data).__name__} to Graph; "
236-
"expected a JSON-LD dict or an rdflib.Graph"
241+
"expected a JSON-LD dict/list or an rdflib.Graph"
237242
)
238243

239244
@staticmethod

tests/unit/test_to_graph.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@
1010

1111
import pytest
1212
from rdflib import Graph, URIRef
13+
from rdflib.namespace import RDF
1314

1415
from web_algebra.operation import Operation
1516

1617
DOC_URI = URIRef("https://example.org/doc/")
1718
PROV_GENERATED_BY = URIRef("http://www.w3.org/ns/prov#wasGeneratedBy")
1819
ACTIVITY_URI = URIRef("https://example.org/activity/#this")
20+
DOC_TYPE = URIRef("https://example.org/T")
1921

2022

2123
class TestToGraph:
@@ -30,6 +32,20 @@ def test_dict_is_parsed_as_jsonld(self):
3032
assert isinstance(graph, Graph)
3133
assert (DOC_URI, PROV_GENERATED_BY, ACTIVITY_URI) in graph
3234

35+
def test_top_level_array_is_parsed_as_jsonld(self):
36+
# SPARQL DESCRIBE/CONSTRUCT over multiple subjects returns a top-level
37+
# JSON-LD array of nodes; to_graph must accept it, not raise.
38+
data = [
39+
{"@id": str(DOC_URI), "@type": [str(DOC_TYPE)]},
40+
{"@id": str(ACTIVITY_URI), str(PROV_GENERATED_BY): {"@id": str(DOC_URI)}},
41+
]
42+
43+
graph = Operation.to_graph(data)
44+
45+
assert isinstance(graph, Graph)
46+
assert (DOC_URI, RDF.type, DOC_TYPE) in graph
47+
assert (ACTIVITY_URI, PROV_GENERATED_BY, DOC_URI) in graph
48+
3349
def test_graph_passes_through_unchanged(self):
3450
# An upstream op (e.g. CONSTRUCT) already produced a Graph — identity.
3551
g = Graph()

0 commit comments

Comments
 (0)