Skip to content

Commit e1791bf

Browse files
committed
When expanding Type maps, apply any context associated with the type from the map.
1 parent aa76f1f commit e1791bf

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

lib/json/ld/expand.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,8 +377,13 @@ def expand_object(input, active_property, context, output_object, ordered: false
377377
# For each key-value in the object:
378378
keys = ordered ? value.keys.sort : value.keys
379379
keys.each do |k|
380+
# If container mapping in the active context is @type, and k is a term in the active context having a local context, use that context when expanding values
381+
map_context = active_context.term_definitions[k].context if container == '@type' && active_context.term_definitions[k]
382+
map_context = active_context.parse(map_context) if map_context
383+
map_context ||= active_context
384+
380385
# Initialize index value to the result of using this algorithm recursively, passing active context, key as active property, and index value as element.
381-
index_value = expand([value[k]].flatten, key, active_context, ordered: ordered)
386+
index_value = expand([value[k]].flatten, key, map_context, ordered: ordered)
382387
#require 'byebug'; byebug
383388
index_value.each do |item|
384389
case container

spec/compact_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,28 @@
11971197
"c": "C in example"
11981198
}),
11991199
},
1200+
"with @container: @type" => {
1201+
input: %([{
1202+
"http://example/typemap": [
1203+
{"http://example.org/a": [{"@value": "Object with @type <Type>"}], "@type": ["http://example/Type"]}
1204+
]
1205+
}]),
1206+
context: %({
1207+
"@vocab": "http://example/",
1208+
"typemap": {"@container": "@type"},
1209+
"Type": {"@context": {"a": "http://example.org/a"}}
1210+
}),
1211+
output: %({
1212+
"@context": {
1213+
"@vocab": "http://example/",
1214+
"typemap": {"@container": "@type"},
1215+
"Type": {"@context": {"a": "http://example.org/a"}}
1216+
},
1217+
"typemap": {
1218+
"Type": {"a": "Object with @type <Type>"}
1219+
}
1220+
})
1221+
},
12001222
}.each_pair do |title, params|
12011223
it(title) {run_compact(params)}
12021224
end

spec/expand_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,6 +1371,23 @@
13711371
"http://example/c": [{"@value": "C in example"}]
13721372
}])
13731373
},
1374+
"with @container: @type" => {
1375+
input: %({
1376+
"@context": {
1377+
"@vocab": "http://example/",
1378+
"typemap": {"@container": "@type"},
1379+
"Type": {"@context": {"a": "http://example.org/a"}}
1380+
},
1381+
"typemap": {
1382+
"Type": {"a": "Object with @type <Type>"}
1383+
}
1384+
}),
1385+
output: %([{
1386+
"http://example/typemap": [
1387+
{"http://example.org/a": [{"@value": "Object with @type <Type>"}], "@type": ["http://example/Type"]}
1388+
]
1389+
}])
1390+
},
13741391
}.each do |title, params|
13751392
it(title) {run_expand params}
13761393
end

0 commit comments

Comments
 (0)