diff --git a/lib/json/ld/compact.rb b/lib/json/ld/compact.rb index 1b80a66..ed6e878 100644 --- a/lib/json/ld/compact.rb +++ b/lib/json/ld/compact.rb @@ -299,10 +299,10 @@ def compact(element, else index_key = context.expand_iri(index_key, vocab: true) container_key = context.compact_iri(index_key, vocab: true) - map_key, *others = Array(compacted_item[container_key]) + map_key, *others = Array(compacted_item.is_a?(Hash) && compacted_item[container_key]) if map_key.is_a?(String) case others.length - when 0 then compacted_item.delete(container_key) + when 0 then compacted_item.delete(container_key) if compacted_item.is_a?(Hash) when 1 then compacted_item[container_key] = others.first else compacted_item[container_key] = others end @@ -316,15 +316,15 @@ def compact(element, map_key = expanded_item['@language'] value?(expanded_item) ? expanded_item['@value'] : compacted_item elsif container.include?('@type') - map_key, *types = Array(compacted_item[container_key]) + map_key, *types = Array(compacted_item.is_a?(Hash) && compacted_item[container_key]) case types.length - when 0 then compacted_item.delete(container_key) + when 0 then compacted_item.delete(container_key) if compacted_item.is_a?(Hash) when 1 then compacted_item[container_key] = types.first else compacted_item[container_key] = types end # if compacted_item contains a single entry who's key maps to @id, then recompact the item without @type - if compacted_item.keys.length == 1 && expanded_item.key?('@id') + if compacted_item.is_a?(Hash) && compacted_item.keys.length == 1 && expanded_item.key?('@id') compacted_item = compact({ '@id' => expanded_item['@id'] }, base: base, property: item_active_property, diff --git a/spec/compact_spec.rb b/spec/compact_spec.rb index e89382e..950301b 100644 --- a/spec/compact_spec.rb +++ b/spec/compact_spec.rb @@ -3448,6 +3448,40 @@ } }), processingMode: "json-ld-1.1" + }, + "ruby-rdf/json-ld#62": { + input: %({ + "@context": { + "@vocab": "http://schema.org/" + }, + "@type": "Event", + "location": { + "@id": "http://kg.artsdata.ca/resource/K11-200" + } + }), + context: %({ + "@context": { + "@vocab": "http://schema.org/", + "location": { + "@type": "@id", + "@container": "@type" + } + } + }), + output: %({ + "@context": { + "@vocab": "http://schema.org/", + "location": { + "@type": "@id", + "@container": "@type" + } + }, + "@type": "Event", + "location": { + "@none": "http://kg.artsdata.ca/resource/K11-200" + } + }), + processingMode: "json-ld-1.1" } }.each do |title, params| it title do diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index 72ed82c..b51abe0 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -2506,6 +2506,41 @@ } }), processingMode: "json-ld-1.1" + }, + "ruby-rdf/json-ld#62": { + input: %({ + "@context": { + "@vocab": "http://schema.org/" + }, + "@type": "Event", + "location": { + "@id": "http://kg.artsdata.ca/resource/K11-200" + } + }), + frame: %({ + "@context": { + "@vocab": "http://schema.org/", + "location": { + "@type": "@id", + "@container": "@type" + } + }, + "@type": "Event" + }), + output: %({ + "@context": { + "@vocab": "http://schema.org/", + "location": { + "@type": "@id", + "@container": "@type" + } + }, + "@type": "Event", + "location": { + "@none": "http://kg.artsdata.ca/resource/K11-200" + } + }), + processingMode: "json-ld-1.1" } }.each do |title, params| it title do