Skip to content

Commit aa76f1f

Browse files
committed
For @id and @container maps, ensure that keys are expanded appropriately during expansion, and compacted appropriately during compaction. Add tests verifying this behavior
1 parent e2eb98b commit aa76f1f

File tree

4 files changed

+110
-9
lines changed

4 files changed

+110
-9
lines changed

lib/json/ld/compact.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ def compact(element, property: nil)
175175
when '@id'
176176
id_prop = context.compact_iri('@id', vocab: true, quiet: true)
177177
map_key = compacted_item[id_prop]
178+
map_key = context.compact_iri(map_key, quiet: true)
178179
compacted_item.delete(id_prop)
179180
compacted_item
180181
when '@index'
@@ -186,6 +187,7 @@ def compact(element, property: nil)
186187
when '@type'
187188
type_prop = context.compact_iri('@type', vocab: true, quiet: true)
188189
map_key, *types = Array(compacted_item[type_prop])
190+
map_key = context.compact_iri(map_key, vocab: true, quiet: true)
189191
case types.length
190192
when 0 then compacted_item.delete(type_prop)
191193
when 1 then compacted_item[type_prop] = types.first

lib/json/ld/expand.rb

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def expand_object(input, active_property, context, output_object, ordered: false
137137
keys.each do |key|
138138
# For each key and value in element, ordered lexicographically by key:
139139
value = input[key]
140-
expanded_property = context.expand_iri(key, vocab: true, log_depth: @options[:log_depth])
140+
expanded_property = context.expand_iri(key, vocab: true, quiet: true)
141141

142142
# If expanded property is null or it neither contains a colon (:) nor it is a keyword, drop key by continuing to the next key.
143143
next if expanded_property.is_a?(RDF::URI) && expanded_property.relative?
@@ -164,16 +164,16 @@ def expand_object(input, active_property, context, output_object, ordered: false
164164
# If expanded property is @id and value is not a string, an invalid @id value error has been detected and processing is aborted
165165
e_id = case value
166166
when String
167-
context.expand_iri(value, documentRelative: true, log_depth: @options[:log_depth]).to_s
167+
context.expand_iri(value, documentRelative: true, quiet: true).to_s
168168
when Array
169169
# Framing allows an array of IRIs, and always puts values in an array
170170
raise JsonLdError::InvalidIdValue,
171171
"value of @id must be a string, array of string or hash if framing: #{value.inspect}" unless framing
172-
context.expand_iri(value, documentRelative: true, log_depth: @options[:log_depth]).to_s
172+
context.expand_iri(value, documentRelative: true, quiet: true).to_s
173173
value.map do |v|
174174
raise JsonLdError::InvalidTypeValue,
175175
"@id value must be a string or array of strings for framing: #{v.inspect}" unless v.is_a?(String)
176-
context.expand_iri(v, documentRelative: true, quiet: true, log_depth: @options[:log_depth]).to_s
176+
context.expand_iri(v, documentRelative: true, quiet: true,).to_s
177177
end
178178
when Hash
179179
raise JsonLdError::InvalidIdValue,
@@ -201,10 +201,10 @@ def expand_object(input, active_property, context, output_object, ordered: false
201201
value.map do |v|
202202
raise JsonLdError::InvalidTypeValue,
203203
"@type value must be a string or array of strings: #{v.inspect}" unless v.is_a?(String)
204-
context.expand_iri(v, vocab: true, documentRelative: true, quiet: true, log_depth: @options[:log_depth]).to_s
204+
context.expand_iri(v, vocab: true, documentRelative: true, quiet: true).to_s
205205
end
206206
when String
207-
context.expand_iri(value, vocab: true, documentRelative: true, quiet: true, log_depth: @options[:log_depth]).to_s
207+
context.expand_iri(value, vocab: true, documentRelative: true, quiet: true).to_s
208208
when Hash
209209
# For framing
210210
raise JsonLdError::InvalidTypeValue,
@@ -379,11 +379,18 @@ def expand_object(input, active_property, context, output_object, ordered: false
379379
keys.each do |k|
380380
# Initialize index value to the result of using this algorithm recursively, passing active context, key as active property, and index value as element.
381381
index_value = expand([value[k]].flatten, key, active_context, ordered: ordered)
382+
#require 'byebug'; byebug
382383
index_value.each do |item|
383384
case container
384-
when '@id', '@index' then item[container] ||= k
385-
# If container is @type add the key-value pair (@type-[index]) to item, appending any existing values in item
386-
when '@type' then item[container] = [k].concat(Array(item[container]))
385+
when '@index' then item[container] ||= k
386+
when '@id'
387+
# Expand k document relative
388+
expanded_k = active_context.expand_iri(k, documentRelative: true, quiet: true).to_s
389+
item[container] ||= expanded_k
390+
when '@type'
391+
# Expand k vocabulary relative
392+
expanded_k = active_context.expand_iri(k, vocab: true, documentRelative: true, quiet: true).to_s
393+
item[container] = [expanded_k].concat(Array(item[container]))
387394
end
388395

389396
# Append item to expanded value.

spec/compact_spec.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,28 @@
549549
}
550550
}),
551551
},
552+
"Indexes to object using compact IRI @id" => {
553+
input: %([{
554+
"http://example/idmap": [
555+
{"http://example/label": [{"@value": "Object with @id <foo>"}], "@id": "http://example.org/foo"}
556+
]
557+
}]),
558+
context: %({
559+
"@vocab": "http://example/",
560+
"ex": "http://example.org/",
561+
"idmap": {"@container": "@id"}
562+
}),
563+
output: %({
564+
"@context": {
565+
"@vocab": "http://example/",
566+
"ex": "http://example.org/",
567+
"idmap": {"@container": "@id"}
568+
},
569+
"idmap": {
570+
"ex:foo": {"label": "Object with @id <foo>"}
571+
}
572+
})
573+
},
552574
}.each_pair do |title, params|
553575
it(title) {run_compact(params)}
554576
end
@@ -634,6 +656,26 @@
634656
}
635657
})
636658
},
659+
"Indexes using compacted @type" => {
660+
input: %([{
661+
"http://example/typemap": [
662+
{"http://example/label": [{"@value": "Object with @type <foo>"}], "@type": ["http://example/Foo"]}
663+
]
664+
}]),
665+
context: %({
666+
"@vocab": "http://example/",
667+
"typemap": {"@container": "@type"}
668+
}),
669+
output: %({
670+
"@context": {
671+
"@vocab": "http://example/",
672+
"typemap": {"@container": "@type"}
673+
},
674+
"typemap": {
675+
"Foo": {"label": "Object with @type <foo>"}
676+
}
677+
})
678+
},
637679
}.each_pair do |title, params|
638680
it(title) {run_compact(params)}
639681
end

spec/expand_spec.rb

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,23 @@
736736
]
737737
}])
738738
},
739+
"Adds expanded @id to object" => {
740+
input: %({
741+
"@context": {
742+
"@vocab": "http://example/",
743+
"idmap": {"@container": "@id"}
744+
},
745+
"idmap": {
746+
"foo": {"label": "Object with @id <foo>"}
747+
}
748+
}),
749+
output: %([{
750+
"http://example/idmap": [
751+
{"http://example/label": [{"@value": "Object with @id <foo>"}], "@id": "http://example.org/foo"}
752+
]
753+
}]),
754+
base: "http://example.org/"
755+
},
739756
}.each do |title, params|
740757
it(title) {run_expand params}
741758
end
@@ -785,6 +802,39 @@
785802
]
786803
}])
787804
},
805+
"Adds vocabulary expanded @type to object" => {
806+
input: %({
807+
"@context": {
808+
"@vocab": "http://example/",
809+
"typemap": {"@container": "@type"}
810+
},
811+
"typemap": {
812+
"Foo": {"label": "Object with @type <foo>"}
813+
}
814+
}),
815+
output: %([{
816+
"http://example/typemap": [
817+
{"http://example/label": [{"@value": "Object with @type <foo>"}], "@type": ["http://example/Foo"]}
818+
]
819+
}])
820+
},
821+
"Adds document expanded @type to object" => {
822+
input: %({
823+
"@context": {
824+
"typemap": {"@id": "http://example/typemap", "@container": "@type"},
825+
"label": "http://example/label"
826+
},
827+
"typemap": {
828+
"Foo": {"label": "Object with @type <foo>"}
829+
}
830+
}),
831+
output: %([{
832+
"http://example/typemap": [
833+
{"http://example/label": [{"@value": "Object with @type <foo>"}], "@type": ["http://example.org/Foo"]}
834+
]
835+
}]),
836+
base: "http://example.org/"
837+
},
788838
}.each do |title, params|
789839
it(title) {run_expand params}
790840
end

0 commit comments

Comments
 (0)