Skip to content

Commit be7e7a5

Browse files
committed
When compacting @type or @language maps, account for compacted_item not being a Hash.
Fixes #62.
1 parent 24a67f4 commit be7e7a5

File tree

3 files changed

+74
-5
lines changed

3 files changed

+74
-5
lines changed

lib/json/ld/compact.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,10 +299,10 @@ def compact(element,
299299
else
300300
index_key = context.expand_iri(index_key, vocab: true)
301301
container_key = context.compact_iri(index_key, vocab: true)
302-
map_key, *others = Array(compacted_item[container_key])
302+
map_key, *others = Array(compacted_item.is_a?(Hash) && compacted_item[container_key])
303303
if map_key.is_a?(String)
304304
case others.length
305-
when 0 then compacted_item.delete(container_key)
305+
when 0 then compacted_item.delete(container_key) if compacted_item.is_a?(Hash)
306306
when 1 then compacted_item[container_key] = others.first
307307
else compacted_item[container_key] = others
308308
end
@@ -316,15 +316,15 @@ def compact(element,
316316
map_key = expanded_item['@language']
317317
value?(expanded_item) ? expanded_item['@value'] : compacted_item
318318
elsif container.include?('@type')
319-
map_key, *types = Array(compacted_item[container_key])
319+
map_key, *types = Array(compacted_item.is_a?(Hash) && compacted_item[container_key])
320320
case types.length
321-
when 0 then compacted_item.delete(container_key)
321+
when 0 then compacted_item.delete(container_key) if compacted_item.is_a?(Hash)
322322
when 1 then compacted_item[container_key] = types.first
323323
else compacted_item[container_key] = types
324324
end
325325

326326
# if compacted_item contains a single entry who's key maps to @id, then recompact the item without @type
327-
if compacted_item.keys.length == 1 && expanded_item.key?('@id')
327+
if compacted_item.is_a?(Hash) && compacted_item.keys.length == 1 && expanded_item.key?('@id')
328328
compacted_item = compact({ '@id' => expanded_item['@id'] },
329329
base: base,
330330
property: item_active_property,

spec/compact_spec.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3448,6 +3448,40 @@
34483448
}
34493449
}),
34503450
processingMode: "json-ld-1.1"
3451+
},
3452+
"ruby-rdf/json-ld#62": {
3453+
input: %({
3454+
"@context": {
3455+
"@vocab": "http://schema.org/"
3456+
},
3457+
"@type": "Event",
3458+
"location": {
3459+
"@id": "http://kg.artsdata.ca/resource/K11-200"
3460+
}
3461+
}),
3462+
context: %({
3463+
"@context": {
3464+
"@vocab": "http://schema.org/",
3465+
"location": {
3466+
"@type": "@id",
3467+
"@container": "@type"
3468+
}
3469+
}
3470+
}),
3471+
output: %({
3472+
"@context": {
3473+
"@vocab": "http://schema.org/",
3474+
"location": {
3475+
"@type": "@id",
3476+
"@container": "@type"
3477+
}
3478+
},
3479+
"@type": "Event",
3480+
"location": {
3481+
"@none": "http://kg.artsdata.ca/resource/K11-200"
3482+
}
3483+
}),
3484+
processingMode: "json-ld-1.1"
34513485
}
34523486
}.each do |title, params|
34533487
it title do

spec/frame_spec.rb

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2506,6 +2506,41 @@
25062506
}
25072507
}),
25082508
processingMode: "json-ld-1.1"
2509+
},
2510+
"ruby-rdf/json-ld#62": {
2511+
input: %({
2512+
"@context": {
2513+
"@vocab": "http://schema.org/"
2514+
},
2515+
"@type": "Event",
2516+
"location": {
2517+
"@id": "http://kg.artsdata.ca/resource/K11-200"
2518+
}
2519+
}),
2520+
frame: %({
2521+
"@context": {
2522+
"@vocab": "http://schema.org/",
2523+
"location": {
2524+
"@type": "@id",
2525+
"@container": "@type"
2526+
}
2527+
},
2528+
"@type": "Event"
2529+
}),
2530+
output: %({
2531+
"@context": {
2532+
"@vocab": "http://schema.org/",
2533+
"location": {
2534+
"@type": "@id",
2535+
"@container": "@type"
2536+
}
2537+
},
2538+
"@type": "Event",
2539+
"location": {
2540+
"@none": "http://kg.artsdata.ca/resource/K11-200"
2541+
}
2542+
}),
2543+
processingMode: "json-ld-1.1"
25092544
}
25102545
}.each do |title, params|
25112546
it title do

0 commit comments

Comments
 (0)