Skip to content

Commit 678c502

Browse files
committed
enh: allow id to be added to reference document when missing
1 parent 2db71da commit 678c502

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

reproschema/jsonldutils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
def load_file(path_or_url, started=False, http_kwargs={}):
99
try:
1010
data = jsonld.expand(path_or_url)
11+
if len(data) == 1:
12+
if "@id" not in data[0]:
13+
data[0]["@id"] = path_or_url
1114
except jsonld.JsonLdError as e:
1215
if 'only "http" and "https"' in str(e):
1316
lgr.debug("Reloading with local server")
@@ -18,7 +21,9 @@ def load_file(path_or_url, started=False, http_kwargs={}):
1821
if "port" not in http_kwargs:
1922
raise KeyError("port key missing in http_kwargs")
2023
port = http_kwargs["port"]
21-
base_url = f"http://localhost:{port}/{root}/"
24+
base_url = f"http://localhost:{port}/"
25+
if root:
26+
base_url += f"{root}/"
2227
with open(path_or_url) as json_file:
2328
data = json.load(json_file)
2429
try:
@@ -28,6 +33,9 @@ def load_file(path_or_url, started=False, http_kwargs={}):
2833
finally:
2934
if not started:
3035
stop_server(stop)
36+
if len(data) == 1:
37+
if "@id" not in data[0]:
38+
data[0]["@id"] = base_url + os.path.basename(path_or_url)
3139
else:
3240
raise
3341
return data

0 commit comments

Comments
 (0)