Skip to content

Commit 3b69d9d

Browse files
committed
Throw error when inline contexts try to redefine @context.
1 parent fe9af10 commit 3b69d9d

3 files changed

Lines changed: 33 additions & 6 deletions

File tree

lib/pyld/jsonld.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2798,7 +2798,19 @@ def _prepare_nested_context(self, active_ctx, element, options):
27982798
# Local contexts on the nested node apply before looking for @type,
27992799
# just like they do for an ordinary node object.
28002800
if '@context' in element:
2801-
active_ctx = self._process_context(active_ctx, element['@context'], options)
2801+
local_ctx = element['@context']
2802+
if (
2803+
_is_object(local_ctx)
2804+
and len(local_ctx) == 1
2805+
and '@context' in local_ctx
2806+
):
2807+
raise JsonLdError(
2808+
'Invalid JSON-LD syntax; keywords cannot be overridden.',
2809+
'jsonld.SyntaxError',
2810+
{'context': local_ctx, 'term': '@context'},
2811+
code='keyword redefinition',
2812+
)
2813+
active_ctx = self._process_context(active_ctx, local_ctx, options)
28022814

28032815
# Set the type-scoped context to the context on input, for use later
28042816
type_scoped_ctx = active_ctx

tests/runtests.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -950,10 +950,7 @@ def write(self, filename):
950950
# skip tests where behavior changed for a 1.1 processor
951951
# see JSON-LD 1.0 Errata
952952
'specVersion': ['json-ld-1.0'],
953-
'idRegex': [
954-
# uncategorized
955-
'.*expand-manifest#ter56$',
956-
],
953+
'idRegex': [],
957954
},
958955
'fn': 'expand',
959956
'params': [read_test_url('input'), create_test_options()],
@@ -1023,7 +1020,6 @@ def write(self, filename):
10231020
# well formed
10241021
'.*toRdf-manifest#twf05$',
10251022
# uncategorized
1026-
'.*toRdf-manifest#ter56$',
10271023
'.*toRdf-manifest#tli12$',
10281024
'.*toRdf-manifest#tli14$',
10291025
]

tests/test_jsonld.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,25 @@ def test_value_object_type_null_expands(self):
119119
{"http://example.com/prop": [{"@value": "value"}]}
120120
]
121121

122+
def test_context_keyword_redefinition_fails(self):
123+
"""
124+
A local context must not define @context as a term.
125+
"""
126+
input = {
127+
"@context": {
128+
"@context": {
129+
"p": "ex:p",
130+
},
131+
},
132+
"@id": "ex:1",
133+
"p": "value",
134+
}
135+
136+
with pytest.raises(jsonld.JsonLdError) as exc:
137+
jsonld.expand(input)
138+
139+
assert exc.value.code == 'keyword redefinition'
140+
122141
def test_dropped_keys_complex(self):
123142
"""
124143
Complex example with keys not in the context should correctly store

0 commit comments

Comments
 (0)