Skip to content

Commit ce00252

Browse files
author
Peter Amstutz
committed
Add test for non-scoped identifiers.
1 parent ef27301 commit ce00252

File tree

3 files changed

+123
-1
lines changed

3 files changed

+123
-1
lines changed

schema_salad/jsonld_context.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,51 @@ def salad_to_jsonld_context(j, schema_ctx):
175175
process_type(t, g, context, defaultBase, namespaces, defaultPrefix)
176176

177177
return (context, g)
178+
179+
def fix_jsonld_ids(obj, ids):
180+
if isinstance(obj, dict):
181+
for i in ids:
182+
if i in obj:
183+
obj["@id"] = obj[i]
184+
for v in obj.values():
185+
fix_jsonld_ids(v, ids)
186+
if isinstance(obj, list):
187+
for i in obj:
188+
fix_jsonld_ids(i, ids)
189+
190+
def makerdf(workflow, wf, ctx):
191+
# type: (Union[str, unicode], Union[List[Dict[unicode, Any]], Dict[unicode, Any]], Loader.ContextType) -> Graph
192+
prefixes = {}
193+
idfields = []
194+
for k,v in ctx.iteritems():
195+
if isinstance(v, dict):
196+
url = v["@id"]
197+
else:
198+
url = v
199+
if url == "@id":
200+
idfields.append(k)
201+
doc_url, frg = urlparse.urldefrag(url)
202+
if "/" in frg:
203+
p, _ = frg.split("/")
204+
prefixes[p] = u"%s#%s/" % (doc_url, p)
205+
206+
if isinstance(wf, list):
207+
wf = {
208+
"@context": ctx,
209+
"@graph": wf
210+
}
211+
else:
212+
wf["@context"] = ctx
213+
214+
fix_jsonld_ids(wf, idfields)
215+
216+
g = Graph().parse(data=json.dumps(wf), format='json-ld', location=workflow)
217+
218+
# Bug in json-ld loader causes @id fields to be added to the graph
219+
for s,p,o in g.triples((None, URIRef("@id"), None)):
220+
g.remove((s, p, o))
221+
222+
for k2,v2 in prefixes.iteritems():
223+
g.namespace_manager.bind(k2, v2)
224+
225+
return g

schema_salad/ref_resolver.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,9 @@ def expand_url(self, url, base_url, scoped_id=False, vocab_term=False, scoped_re
135135
frg = splitbase.fragment + u"/" + split.path
136136
else:
137137
frg = split.path
138+
pt = splitbase.path if splitbase.path else "/"
138139
url = urlparse.urlunsplit(
139-
(splitbase.scheme, splitbase.netloc, splitbase.path, splitbase.query, frg))
140+
(splitbase.scheme, splitbase.netloc, pt, splitbase.query, frg))
140141
elif scoped_ref is not None and not split.fragment:
141142
pass
142143
else:

tests/test_examples.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import schema_salad.ref_resolver
33
import schema_salad.main
44
import schema_salad.schema
5+
from schema_salad.jsonld_context import makerdf
56
import rdflib
67
import ruamel.yaml as yaml
8+
import json
9+
710
try:
811
from ruamel.yaml import CSafeLoader as SafeLoader
912
except ImportError:
@@ -249,5 +252,75 @@ def test_typedsl_ref(self):
249252
ra, _ = ldr.resolve_all({"type": "File[]?"}, "")
250253
self.assertEqual({'type': ['null', {'items': 'File', 'type': 'array'}]}, ra)
251254

255+
def test_scoped_id(self):
256+
ldr = schema_salad.ref_resolver.Loader({})
257+
ctx = {
258+
"id": "@id",
259+
"location": {
260+
"@id": "@id",
261+
"@type": "@id"
262+
},
263+
"bar": "http://example.com/bar",
264+
"ex": "http://example.com/"
265+
}
266+
ldr.add_context(ctx)
267+
268+
ra, _ = ldr.resolve_all({
269+
"id": "foo",
270+
"bar": {
271+
"id": "baz"
272+
}
273+
}, "http://example.com")
274+
self.assertEqual({'id': 'http://example.com/#foo',
275+
'bar': {
276+
'id': 'http://example.com/#foo/baz'},
277+
}, ra)
278+
279+
g = makerdf(None, ra, ctx)
280+
print(g.serialize(format="n3"))
281+
282+
ra, _ = ldr.resolve_all({
283+
"location": "foo",
284+
"bar": {
285+
"location": "baz"
286+
}
287+
}, "http://example.com", checklinks=False)
288+
self.assertEqual({'location': 'http://example.com/foo',
289+
'bar': {
290+
'location': 'http://example.com/baz'},
291+
}, ra)
292+
293+
g = makerdf(None, ra, ctx)
294+
print(g.serialize(format="n3"))
295+
296+
ra, _ = ldr.resolve_all({
297+
"id": "foo",
298+
"bar": {
299+
"location": "baz"
300+
}
301+
}, "http://example.com", checklinks=False)
302+
self.assertEqual({'id': 'http://example.com/#foo',
303+
'bar': {
304+
'location': 'http://example.com/baz'},
305+
}, ra)
306+
307+
g = makerdf(None, ra, ctx)
308+
print(g.serialize(format="n3"))
309+
310+
ra, _ = ldr.resolve_all({
311+
"location": "foo",
312+
"bar": {
313+
"id": "baz"
314+
}
315+
}, "http://example.com", checklinks=False)
316+
self.assertEqual({'location': 'http://example.com/foo',
317+
'bar': {
318+
'id': 'http://example.com/#baz'},
319+
}, ra)
320+
321+
g = makerdf(None, ra, ctx)
322+
print(g.serialize(format="n3"))
323+
324+
252325
if __name__ == '__main__':
253326
unittest.main()

0 commit comments

Comments
 (0)