Skip to content

Commit 133bc3d

Browse files
committed
Expand and compact @container: @index where value is a graph.
1 parent 9de3c5b commit 133bc3d

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

lib/json/ld/compact.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def compact(element, property: nil)
201201
# Do not compact using vocabulary-relative, which is not valid for @id
202202
compacted_item[al] = context.compact_iri(expanded_item['@id'], vocab: false, quiet: true).to_s
203203
end
204-
if expanded_item.has_key?('@index')
204+
if expanded_item.has_key?('@index') && container != '@index'
205205
key = context.compact_iri('@index', vocab: true, quiet: true)
206206
compacted_item[key] = expanded_item['@index']
207207
end

spec/compact_spec.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,37 @@
495495

496496
context "@container: @index" do
497497
{
498+
"named graph": {
499+
input: %([
500+
{
501+
"@id": "http://example.com/graphTest",
502+
"http://example.org/container": [{
503+
"@graph": [{"http://example.org/input": [{"@value": "de"}]}],
504+
"@index": "de"
505+
}, {
506+
"@graph": [{"http://example.org/input": [{"@value": "en"}]}],
507+
"@index": "en"
508+
}]
509+
}
510+
]),
511+
context: %({
512+
"@context": {
513+
"@vocab": "http://example.org/",
514+
"container": {"@container": "@index"}
515+
}
516+
}),
517+
output: %({
518+
"@context": {
519+
"@vocab": "http://example.org/",
520+
"container": {"@container": "@index"}
521+
},
522+
"@id": "http://example.com/graphTest",
523+
"container": {
524+
"en": {"@graph": [{"input": "en"}]},
525+
"de": {"@graph": [{"input": "de"}]}
526+
}
527+
})
528+
},
498529
"compact-0029" => {
499530
input: %([{
500531
"@id": "http://example.com/article",
@@ -1642,6 +1673,7 @@ def run_compact(params)
16421673
input = ::JSON.parse(input) if input.is_a?(String)
16431674
output = ::JSON.parse(output) if output.is_a?(String)
16441675
context = ::JSON.parse(context) if context.is_a?(String)
1676+
context = context['@context'] if context.has_key?('@context')
16451677
pending params.fetch(:pending, "test implementation") unless input
16461678
if params[:exception]
16471679
expect {JSON::LD::API.compact(input, context, params.merge(logger: logger))}.to raise_error(params[:exception])

spec/expand_spec.rb

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,31 @@
483483
}
484484
]
485485
},
486+
"graph" => {
487+
input: %({
488+
"@context": {
489+
"@vocab": "http://example.org/",
490+
"container": {"@container": "@index"}
491+
},
492+
"@id": "http://example.com/graphTest",
493+
"container": {
494+
"en": {"@graph": {"input": "en"}},
495+
"de": {"@graph": {"input": "de"}}
496+
}
497+
}),
498+
output: %([
499+
{
500+
"@id": "http://example.com/graphTest",
501+
"http://example.org/container": [{
502+
"@graph": [{"http://example.org/input": [{"@value": "de"}]}],
503+
"@index": "de"
504+
}, {
505+
"@graph": [{"http://example.org/input": [{"@value": "en"}]}],
506+
"@index": "en"
507+
}]
508+
}
509+
])
510+
},
486511
}.each do |title, params|
487512
it(title) {run_expand params}
488513
end

0 commit comments

Comments
 (0)