Skip to content

Commit e82e5a1

Browse files
committed
Use _prepare_nested_context in _expand as well
1 parent 9ae742f commit e82e5a1

1 file changed

Lines changed: 13 additions & 34 deletions

File tree

lib/pyld/jsonld.py

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2134,35 +2134,10 @@ def _expand(
21342134
active_ctx, property_scoped_ctx, options, override_protected=True
21352135
)
21362136

2137-
# recursively expand object
2138-
# if element has a context, process it
2139-
if '@context' in element:
2140-
active_ctx = self._process_context(active_ctx, element['@context'], options)
2141-
2142-
# set the type-scoped context to the context on input, for use later
2143-
type_scoped_ctx = active_ctx
2144-
2145-
# Remember the first key found expanding to @type
2146-
type_key = None
2147-
2148-
# look for scoped context on @type
2149-
for key, _value in sorted(element.items()):
2150-
expanded_property = self._expand_iri(active_ctx, key, vocab=True)
2151-
if expanded_property == '@type':
2152-
if not type_key:
2153-
type_key = key
2154-
# set scoped contexts from @type
2155-
types = [
2156-
t for t in JsonLdProcessor.arrayify(element[key]) if _is_string(t)
2157-
]
2158-
for type_ in sorted(types):
2159-
ctx = JsonLdProcessor.get_context_value(
2160-
type_scoped_ctx, type_, '@context'
2161-
)
2162-
if ctx is not None and ctx is not False:
2163-
active_ctx = self._process_context(
2164-
active_ctx, ctx, options, propagate=False
2165-
)
2137+
# prepare type-scoped contexts
2138+
active_ctx, type_key, type_scoped_ctx = self._prepare_nested_context(
2139+
active_ctx, element, options
2140+
)
21662141

21672142
# process each key and value in element, ignoring @nest content
21682143
rval = {}
@@ -2819,24 +2794,28 @@ def _prepare_nested_context(self, active_ctx, element, options):
28192794
a scoped context on the nest term must be active while discovering any
28202795
@type entries and applying the type-scoped contexts they reference.
28212796
"""
2797+
# Recursively expand object: process context if element has one
28222798
# Local contexts on the nested node apply before looking for @type,
28232799
# just like they do for an ordinary node object.
28242800
if '@context' in element:
2825-
active_ctx = self._process_context(
2826-
active_ctx, element['@context'], options
2827-
)
2801+
active_ctx = self._process_context(active_ctx, element['@context'], options)
28282802

2829-
# Type terms are looked up against the context that was active before
2830-
# applying any type-scoped contexts discovered below.
2803+
# Set the type-scoped context to the context on input, for use later
28312804
type_scoped_ctx = active_ctx
2805+
2806+
# Remember the first key found expanding to @type
28322807
type_key = None
2808+
2809+
# Type terms are looked up against the context that was active before
2810+
# applying any type-scoped contexts discovered below.
28332811
for key, value in sorted(element.items()):
28342812
# The @type entry itself may be aliased by the nest term's scoped
28352813
# context, so use the nested active context to identify it.
28362814
if self._expand_iri(active_ctx, key, vocab=True) != '@type':
28372815
continue
28382816

28392817
type_key = type_key or key
2818+
# set scoped contexts from @type
28402819
types = [t for t in JsonLdProcessor.arrayify(value) if _is_string(t)]
28412820
for type_ in sorted(types):
28422821
ctx = JsonLdProcessor.get_context_value(

0 commit comments

Comments
 (0)