Skip to content

Commit 18d8a91

Browse files
committed
Allow duplicate entries for @type.
1 parent 345b7a5 commit 18d8a91

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

lib/json/ld/expand.rb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,13 @@ def expand(input, active_property, context, ordered: false, framing: false, from
8686
output_object = {}
8787

8888
# See if keys mapping to @type have terms with a local context
89-
type_key = input.keys.detect {|k| context.expand_iri(k, vocab: true, quite: true) == '@type'}
90-
if type_key
91-
Array(input[type_key]).sort.each do |term|
89+
type_key = nil
90+
input.keys.sort.
91+
select {|k| context.expand_iri(k, vocab: true, quite: true) == '@type'}.
92+
each do |tk|
93+
94+
type_key ||= tk # Side effect saves the first found key mapping to @type
95+
Array(input[tk]).sort.each do |term|
9296
term_context = type_scoped_context.term_definitions[term].context if type_scoped_context.term_definitions[term]
9397
context = term_context ? context.parse(term_context, propagate: false) : context
9498
end
@@ -215,9 +219,9 @@ def expand_object(input, active_property, context, output_object,
215219
raise JsonLdError::InvalidReversePropertyMap,
216220
"@reverse not appropriate at this point" if expanded_active_property == '@reverse'
217221

218-
# If result has already an expanded property member, an colliding keywords error has been detected and processing is aborted.
222+
# If result has already an expanded property member (other than @type), an colliding keywords error has been detected and processing is aborted.
219223
raise JsonLdError::CollidingKeywords,
220-
"#{expanded_property} already exists in result" if output_object.has_key?(expanded_property)
224+
"#{expanded_property} already exists in result" if output_object.has_key?(expanded_property) && expanded_property != '@type'
221225

222226
expanded_value = case expanded_property
223227
when '@id'
@@ -276,12 +280,9 @@ def expand_object(input, active_property, context, output_object,
276280
"@type value must be a string or array of strings: #{value.inspect}"
277281
end
278282

283+
e_type = Array(output_object['@type']) + Array(e_type)
279284
# Use array form if framing
280-
if framing
281-
as_array(e_type)
282-
else
283-
e_type
284-
end
285+
framing || e_type.length > 1 ? e_type : e_type.first
285286
when '@graph'
286287
# If expanded property is @graph, set expanded value to the result of using this algorithm recursively passing active context, @graph for active property, and value for element.
287288
value = expand(value, '@graph', context, ordered: ordered, framing: framing)

0 commit comments

Comments
 (0)