diff options
author | Iwan Aucamp <aucampia@gmail.com> | 2023-03-24 19:17:16 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-24 19:17:16 +0100 |
commit | 832e6931af6be32f71993088d70ae93a5cb9cb05 (patch) | |
tree | 8994a9d359e0bcf89d7c454cfe808a254d99d9d4 | |
parent | 1ab4fc0533d8042e06d7201d7d80d2f41cda83d7 (diff) | |
download | rdflib-832e6931af6be32f71993088d70ae93a5cb9cb05.tar.gz |
fix: JSON-LD context construction from a `dict` (#2306)
A variable was only being initialized for string-valued inputs, but if a `dict`
input was passed the variable would still be accessed, resulting in a
`UnboundLocalError`.
This change initializes the variable always, instead of only when string-valued
input is used to construct a JSON-LD context.
- Closes <https://github.com/RDFLib/rdflib/issues/2303>.
-rw-r--r-- | rdflib/plugins/shared/jsonld/context.py | 2 | ||||
-rw-r--r-- | test/jsonld/test_context.py | 21 |
2 files changed, 22 insertions, 1 deletions
diff --git a/rdflib/plugins/shared/jsonld/context.py b/rdflib/plugins/shared/jsonld/context.py index 23ab6db2..d0224d4a 100644 --- a/rdflib/plugins/shared/jsonld/context.py +++ b/rdflib/plugins/shared/jsonld/context.py @@ -421,13 +421,13 @@ class Context(object): ): for source in inputs: source_url = in_source_url + new_base = base if isinstance(source, str): source_url = source source_doc_base = base or self.doc_base new_ctx = self._fetch_context( source, source_doc_base, referenced_contexts ) - new_base = base if new_ctx is None: continue else: diff --git a/test/jsonld/test_context.py b/test/jsonld/test_context.py index b7628fb3..6578268a 100644 --- a/test/jsonld/test_context.py +++ b/test/jsonld/test_context.py @@ -3,6 +3,7 @@ JSON-LD Context Spec """ from functools import wraps +from pathlib import Path from typing import Any, Dict from rdflib.plugins.shared.jsonld import context, errors @@ -213,3 +214,23 @@ def test_invalid_remote_context(): ctx_url = "http://example.org/recursive.jsonld" SOURCES[ctx_url] = {"key": "value"} ctx = Context(ctx_url) + + +def test_file_source(tmp_path: Path) -> None: + """ + A file URI source to `Context` gets processed correctly. + """ + file = tmp_path / "context.jsonld" + file.write_text(r"""{ "@context": { "ex": "http://example.com/" } }""") + ctx = Context(source=file.as_uri()) + assert "http://example.com/" == ctx.terms["ex"].id + + +def test_dict_source(tmp_path: Path) -> None: + """ + A dictionary source to `Context` gets processed correctly. + """ + file = tmp_path / "context.jsonld" + file.write_text(r"""{ "@context": { "ex": "http://example.com/" } }""") + ctx = Context(source=[{"@context": file.as_uri()}]) + assert "http://example.com/" == ctx.terms["ex"].id |