Skip to content
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- An empty property-scoped context no longer resets the active context in `_create_term_definition`. Now only explicit null becomes False; empty contexts are preserved. Fixes [compact#tc028](https://w3c.github.io/json-ld-api/tests/compact-manifest.html#tc028), [toRdf#tc036](https://w3c.github.io/json-ld-api/tests/toRdf-manifest.html#tc036) and [expand#tc036](https://w3c.github.io/json-ld-api/tests/expand-manifest.html#tc036).
- Options from the test manifest now override the options configured in `create_test_options()`, instead of the other way around. This fixes tests not able to override default options in the test-setup such as `extractAllScripts`. Fixes [html#tf004](https://w3c.github.io/json-ld-api/tests/html-manifest#tf004).
- When `@type` is `@json` in a frame, it no longer raises an "Invalid JSON-LD syntax" error. Fixes [frame#t0069](https://w3c.github.io/json-ld-framing/tests/frame-manifest.html#t0069).
- Use safeguard for non-dict values of `options['link']`

### Added
- `pyld.DocumentLoader` abstract base class for class-based document loaders,
Expand Down
11 changes: 8 additions & 3 deletions lib/pyld/jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -1496,7 +1496,8 @@ def _compact(self, active_ctx, active_property, element, options):
# recursively compact object
if _is_object(element):
if (
options['link']
isinstance(options['link'], dict)
and options['link']
and '@id' in element
and element['@id'] in options['link']
):
Expand All @@ -1511,7 +1512,7 @@ def _compact(self, active_ctx, active_property, element, options):
rval = self._compact_value(
active_ctx, active_property, element, options
)
if options['link'] and _is_subject_reference(element):
if isinstance(options['link'], dict) and options['link'] and _is_subject_reference(element):
# store linked element
options['link'].setdefault(element['@id'], []).append(
{'expanded': element, 'compacted': rval}
Expand Down Expand Up @@ -1556,7 +1557,11 @@ def _compact(self, active_ctx, active_property, element, options):
override_protected=True,
)

if options['link'] and '@id' in element:
if (
isinstance(options['link'], dict)
and options['link']
and '@id' in element
):
# store linked element
options['link'].setdefault(element['@id'], []).append(
{'expanded': element, 'compacted': rval}
Expand Down
60 changes: 60 additions & 0 deletions tests/test_jsonld.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,66 @@ def test_compact_dates_with_datatype(self):
framed = jsonld.frame(input, frame)
assert framed == expected

def test_circular_references_link_and_embed(self):
input = {
"@context": "http://schema.org/",
"@type": "Person",
"name": "Jane Doe",
"jobTitle": "Professor",
"telephone": "(425) 123-4567",
"@id": "http://www.janedoe.com",
"knows": {
"name": "John Smith",
"@type": "Person",
"@id": "http://www.johnsmith.me",
"knows": {"@id": "http://www.janedoe.com"},
},
}

expected = {
"@context": "http://schema.org",
"@graph": [
{
"id": "http://www.janedoe.com",
"type": "Person",
"jobTitle": "Professor",
"knows": {
"id": "http://www.johnsmith.me",
"type": "Person",
"knows": {
"id": "http://www.janedoe.com"
},
"name": "John Smith"
},
"name": "Jane Doe",
"telephone": "(425) 123-4567"
},
{
"id": "http://www.johnsmith.me",
"type": "Person",
"knows": {
"id": "http://www.janedoe.com",
"type": "Person",
"jobTitle": "Professor",
"knows": {
"id": "http://www.johnsmith.me"
},
"name": "Jane Doe",
"telephone": "(425) 123-4567"
},
"name": "John Smith"
}
]
}

frame = {'@context': 'http://schema.org', '@embed': '@once'}
assert expected == jsonld.frame(input, frame)

# this should result in a RuntimeError for exceeding recursion depth
frame = {'@context': 'http://schema.org', '@embed': '@link'}
with pytest.raises(RecursionError):
jsonld.frame(input, frame)


class TestToRdf:
# PR: https://github.com/digitalbazaar/pyld/pull/202
Expand Down
Loading